我不确定您要问什么,但似乎您想调整两个图像的大小并保持两个图像之间的长宽比.如果是这样,这是一个调整图像大小并将纵横比保持为任意宽度或高度的功能.
import cv2
# Resizes a image and maintains aspect ratio
def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# Grab the image size and initialize dimensions
dim = None
(h, w) = image.shape[:2]
# Return original image if no need to resize
if width is None and height is None:
return image
# We are resizing height if 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)
# We are resizing width if height is none
else:
# Calculate the ratio of the 0idth and construct the dimensions
r = width / float(w)
dim = (width, int(h * r))
# Return the resized image
return cv2.resize(image, dim, interpolation=inter)
if __name__ == '__main__':
image = cv2.imread('../color_palette.jpg')
cv2.imshow('image', image)
cv2.waitKey(0)
resized = maintain_aspect_ratio_resize(image, width=400)
cv2.imshow('resized', resized)
cv2.waitKey(0)
您可能需要改写您的问题,以使问题更加清楚.