我有一个函数来调整给定图像的大小。但是,我想做的是在其中使用 for 循环,这样,我可以根据需要调整任意数量的图像大小并将它们保存在不同的变量中。
这里有从opencv读取代码的图像。
image1 = cv2.imread(test1.jpg)
image2 = cv2.imread(test2.jpg)
下面是图像大小调整功能。目前,它采用图像1,调整大小,然后将其保存到img1。如何使用 for 循环,以便我可以传递图像 1、图像 2、图像 3 等,并将其保存到 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)
问这个问题可能是愚蠢的。但我是这个编程领域的新手。因此,任何帮助将不胜感激。
这是使用 for 循环的一种方法,假设您有 10 张图像作为示例。
说明:创建一个空列表resized_images
来存储调整大小的图像。假设您有 10 张名为 test1.jpg
、test2.jpg
、test3.jpg
等的图像。使用索引i
循环访问 10 个值,然后在 for 循环中使用imread
读取图像并调用该函数。函数的返回值现在存储在名为 resized_images
的列表中,稍后可以访问该列表。 'test%s.jpg' %i
是动态读取具有变量名称的不同图像的方法。
现在,一旦调整了所有图像的大小,您就可以访问第一个调整大小的图像作为resized_images[0]
,第二个调整大小的图像作为resized_images[1]
,依此类推。python 中的索引从 0 开始,因此使用索引 [0]
访问第一个图像。
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
dim = None
(h, w) = image.shape[:2]
if width is None and height is None:
return image
if width is None:
r = height / float(h)
dim = (int(w * r), height)
else:
r = width / float(w)
dim = (width, int(h * r))
resized = cv2.resize(image, dim, interpolation = inter)
return resized
resized_images = []
number_of_images = 10
for i in range(1, number_of_images+1):
image = cv2.imread('test%s.jpg' %i)
img = image_resize(image, height = 500)
resized_images.append(img)
创建一个字典并定义图像详细信息,这些细节将传递给函数image_resize
并使用循环将有所帮助。
这是示例代码
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
if 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
resized_images = []
#here image_name is name of the images,
# in image_width/image_height add width/height according to image
obj = { image: [image_name],
width :[image_width],
height:[image_heights],
inter =cv2.INTER_AREA
}
def fun(obj):
for i in obj:
for j in i:
img = image_resize(i['image'][j],i['width'][j],i['height'][j],i['inter'])
resized_image.append(img)
fun(obj)