我有一个调整给定图像大小的功能。但是,我想做的是在其中使用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)
问这可能是愚蠢的问题。但是我是这个编程领域的新手。因此,将不胜感激。
答案 0 :(得分:0)
这里是使用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)
答案 1 :(得分:0)
创建字典并定义图像细节,将其传递给函数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)