700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 【数字图像处理】Python使用PIL库压缩图片大小——按比例压缩

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

时间:2019-12-30 20:14:10

相关推荐

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

方法

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。