简体   繁体   English

PIL调整大小图像

[英]PIL resizing image

image.thumbnail((128, 128), Image.ANTIALIAS) image.thumbnail((128,128),Image.ANTIALIAS)

PIL sets the height of the new image to the size given(128 here) and calculate the width to keep the aspect ratio. PIL将新图像的高度设置为给定的大小(此处为128),并计算宽度以保持宽高比。

Is there a way to set width to 128 and let it calculate height to keep the aspect ratio? 有没有一种方法可以将width设置为128并让其计算高度以保持纵横比?

well if all else fails, you can always just use python directly as the calculator: 如果所有其他方法都失败了,那么您始终可以直接将python直接用作计算器:

width_ratio = image.size[0]/128.0
new_height = image.size[1]/width_ratio

According to the documentation thumbnail method: 根据文档缩略图方法:

Modifies the image to contain a thumbnail version of itself, no larger than the given size.

Try setting width to 128 and using a big number (eg 10000) for height. 尝试将宽度设置为128,并使用一个较大的数字(例如10000)作为高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM