方法
网上的都是按照固定的图像大小来进行压缩,本文给出按照比例来压缩的方法——智能压缩:
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