方法
网上的都是按照固定的图像大小来进行压缩,本文给出按照比例来压缩的方法——智能压缩:
from PIL import Imageinfile = 'cxq1.jpg'outfile = 'cxq2.jpg'im = Image.open(infile)(x,y) = im.size #read image sizex_s = 1000 #define standard widthy_s = int(y * x_s / x) #calc height based on standard widthout = im.resize((x_s,y_s)) #resize image with high-qualityout.save(outfile)print('original size: ',x,y)print('adjust size: ',x_s,y_s)
例子
比如输入原图像尺寸:
original size: 2185 3008
最终输出的尺寸:
adjust size: 1000 1376