如何使用PIL调整图像大小并保持其长宽比?
0 1516
2

有没有一种好的方法?我想制作缩略图。

收藏
2021-01-27 13:24 更新 karry •  4540
共 1 个回答
高赞 时间
0

定义最大尺寸,然后,通过计算调整比例min(maxwidth/width, maxheight/height)。 适当的尺寸是oldsize*ratio。

当然,还有一个方法可以做到这一点:method Image.thumbnail。 以下是PIL文档中的一个(经过编辑的)示例。

import os, sys
import Image

size = 128, 128

for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".thumbnail"
    if infile != outfile:
        try:
            im = Image.open(infile)
            im.thumbnail(size, Image.ANTIALIAS)
            im.save(outfile, "JPEG")
        except IOError:
            print "cannot create thumbnail for '%s'" % infile

Via:https://stackoverflow.com/a/273962/14964791

收藏
2021-01-27 14:27 更新 anna •  5042