【数字图像处理】Python使用PIL库压缩图片大小——按比例压缩

方法

网上的都是按照固定的图像大小来进行压缩,本文给出按照比例来压缩的方法——智能压缩:

from PIL import Image

infile = 'cxq1.jpg'
outfile = 'cxq2.jpg'
im = Image.open(infile)
(x,y) = im.size #read image size
x_s = 1000 #define standard width
y_s = int(y * x_s / x) #calc height based on standard width
out = im.resize((x_s,y_s)) #resize image with high-quality
out.save(outfile)

print('original size: ',x,y)
print('adjust size: ',x_s,y_s)

例子

  • 比如输入原图像尺寸:
    original size: 2185 3008

  • 最终输出的尺寸:
    adjust size: 1000 1376

猜你喜欢

转载自blog.csdn.net/weixin_39589455/article/details/124649609