python opencv压缩图片大小_使用OpenCV Python调整图像大小的最佳方法

我想你是想调整和保持纵横比。这里有一个函数可以根据百分比来放大或缩小图像

原始图像示例

Bmm5b.png

将图像大小调整为0.5(50%)

fsIqY.png

将图像大小调整为1.3(130%)

QLJm3.pngimport 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 width 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('1.png')

cv2.imshow('image', image)

resize_ratio = 1.2

resized = maintain_aspect_ratio_resize(image, width=int(image.shape[1] * resize_ratio))

cv2.imshow('resized', resized)

cv2.imwrite('resized.png', resized)

cv2.waitKey(0)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值