python图片压缩不失真_调整图像大小而不失真OpenCV

你可以在下面试试。该函数将保持原始图像的纵横比。def image_resize(image, 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) = image.shape[:2]

# if both the width and height are None, then return the

# original image

if width is None and height is None:

return image

# 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(image, dim, interpolation = inter)

# return the resized image

return resized

下面是一个使用示例。image = image_resize(image, height = 800)

希望这有帮助。


版权声明:本文为weixin_36108913原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。