【发布时间】:2014-09-04 22:04:39
【问题描述】:
假设我有一个 2322 像素 x 4128 像素的图像。如何缩放它以使宽度和高度都小于 1028 像素?
我将无法使用Image.resize (https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.resize),因为这需要我同时提供新的宽度和高度。我打算做的是(下面的伪代码):
if (image.width or image.height) > 1028:
if image.width > image.height:
tn_image = image.scale(make width of image 1028)
# since the height is less than the width and I am scaling the image
# and making the width less than 1028px, the height will surely be
# less than 1028px
else: #image's height is greater than it's width
tn_image = image.scale(make height of image 1028)
我猜我需要使用Image.thumbnail,但根据此示例 (http://pillow.readthedocs.org/en/latest/reference/Image.html#create-thumbnails) 和此答案 (How do I resize an image using PIL and maintain its aspect ratio?),提供了宽度和高度以创建缩略图。是否有任何函数可以采用新宽度或新高度(不是两者)并缩放整个图像?
【问题讨论】:
-
向
Image.thumbnail提供宽度和高度有什么问题?你想用你的自定义代码做到这一点。
标签: python python-imaging-library image-scaling pillow