我正在编写一个程序来调整输入图像的大小。缩小图像没有问题。但是,我想知道是否有一种方法可以仅在图像宽度大于1000px时才调整图像的大小?
代码:
import numpy as np
import cv2
import easygui
def openTestImg():
global testImg
file = easygui.fileopenbox()
testImg = cv2.imread(file)
def resizeImg():
global rImg, width, height
print('original dimensions: ', testImg.shape)
width, height, channels = testImg.shape
scale_percent = 80 # percent of original size
width = int(testImg.shape[1] * scale_percent / 100)
height = int(testImg.shape[0] * scale_percent / 100)
dim = (width, height)
# resize image
rImg = cv2.resize(testImg, dim, interpolation = cv2.INTER_AREA)
print('Resized Dimensions : ', rImg.shape)
openTestImg()
resizeImg()
这应该可行。您可以设置一条if语句(在调整大小函数的第三行之后),该语句检查width变量是否小于1000,然后如果为true,则返回原始图像。否则它将运行其余的代码