2 回答
TA贡献1906条经验 获得超3个赞
创建字典并定义图像详细信息,将其传递给函数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)
TA贡献1856条经验 获得超11个赞
这是使用 for 循环的一种方法,假设您有 10 张图像作为示例。
说明:创建一个空列表resized_images来存储调整大小的图像。让我们假设你有一个名为10倍的图像test1.jpg,test2.jpg,test3.jpg等等。您使用索引i迭代 10 个值,然后使用imreadfor 循环读取图像并调用该函数。该函数的返回值现在存储在一个列表中resized_images,您可以稍后访问该列表。'test%s.jpg' %i是动态读取具有变量名称的不同图像的方法。
现在一旦所有图像都调整了大小,您可以访问第一个调整大小的图像resized_images[0],第二个调整大小的图像resized_images[1]等等。python 中的索引从 0 开始,因此使用 index 访问第一个图像[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)
添加回答
举报