gpt4 book ai didi

python - 在 PhotoImage 下调整图像大小

转载 作者:太空狗 更新时间:2023-10-29 17:05:08 26 4
gpt4 key购买 nike

我需要调整图像大小,但我想避免 PIL,因为我不能让它在 OS X 下工作 - 不要问我为什么......

无论如何,因为我对 gif/pgm/ppm 很满意,所以 PhotoImage 类对我来说没问题:

photoImg = PhotoImage(file=imgfn)
images.append(photoImg)
text.image_create(INSERT, image=photoImg)

问题是 - 如何调整图像的大小?以下仅适用于 PIL,这是非 PIL 等效项?

img = Image.open(imgfn)
img = img.resize((w,h), Image.ANTIALIAS)
photoImg = ImageTk.PhotoImage(img)
images.append(photoImg)
text.image_create(INSERT, image=photoImg)

谢谢!

最佳答案

因为 zoom()subsample() 都需要整数作为参数,所以我使用了两者。

我不得不将 320x320 的图像调整为 250x250,我最终得到了

imgpath = '/path/to/img.png'
img = PhotoImage(file=imgpath)
img = img.zoom(25) #with 250, I ended up running out of memory
img = img.subsample(32) #mechanically, here it is adjusted to 32 instead of 320
panel = Label(root, image = img)

关于python - 在 PhotoImage 下调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6582387/

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