我有一个功能可以调整给定图像的大小。但是,我想要做的是,在其中使用 for 循环,这样我就可以根据需要调整尽可能多的图像大小并将它们保存在不同的变量中。
这里我有来自opencv的图像读取代码。
image1 = cv2.imread(test1.jpg)
image2 = cv2.imread(test2.jpg)
下面是图像大小调整功能。目前,它采用 image1,调整大小,然后将其保存到 img1。如何使用 for 循环,以便我可以传递 image1、image2、image3 等,并将其分别保存到 img1、img2、img3.....
def image_resize(image1, width=None, height=None,inter=cv2.INTER_AREA):
# initialize the dimensions of the image to be resized and
# grab the image size
dim = None
(h, w) = image1.shape[:2]
# if both the width and height are None, then return the
# original image
width is None and height is None:
return image1
# check to see if the width is None
if width is None:
# calculate the ratio of the height and construct the dimensions
r = height / float(h)
dim = (int(w * r), height)
# otherwise, the height is None
else:
# calculate the ratio of the width and construct the dimensions
r = width / float(w)
dim = (width, int(h * r))
# resize the image
resized = cv2.resize(image1, dim, interpolation = inter)
# return the resized image
return resized
img1 = image_resize(image1, height = 500)
问这个问题可能很愚蠢。但我是这个编程区的新手。因此,任何帮助将不胜感激。
一只名叫tom的猫
呼唤远方
相关分类