gpt4 book ai didi

python - PIL 中至少具有一定尺寸的图像缩略图

转载 作者:行者123 更新时间:2023-12-01 03:23:14 27 4
gpt4 key购买 nike

我正在尝试调整图像大小,保持其纵横比,使其最小尺寸为 500px .

使用 this 中的代码答案有效,但它根据最大尺寸调整图像大小。

如果我们有 2000x1000图像,我们设置 size= 500,500生成的图像将是 500x250 ,而我渴望 1000x500 。我该怎么做?

显然这些是随机数。

最佳答案

使用max而不是min。观察不同的结果:

>>> maxwidth = 500
>>> maxheight = 500
>>> width = 2000
>>> height = 1000
>>> i = min(maxwidth/width, maxheight/height)
>>> a = max(maxwidth/width, maxheight/height)
>>> width*i, height*i
(500.0, 250.0)
>>> width*a, height*a
(1000.0, 500.0)

您可以使用这些值来确定应发送到 img.thumbnail 的大小:

x,y = 500, 500
# compute i and a above
m.thumbnail((x*a/i, y*a/i), Image.ANTIALIAS)

关于python - PIL 中至少具有一定尺寸的图像缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41720557/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com