在 PhotoImage 下调整图像大小 [英] Image resize under PhotoImage

查看:65
本文介绍了在 PhotoImage 下调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to resize an image, but I want to avoid PIL, since I cannot make it work under OS X - don't ask me why...

无论如何,因为我对 gif/pgm/ppm 感到满意,所以 PhotoImage 类对我来说还可以:

Anyway since I am satisfied with gif/pgm/ppm, the PhotoImage class is ok for me:

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

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

The problem is - how do I resize the image? The following works only with PIL, which is the non-PIL equivalent?

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() 都需要整数作为参数,所以我都使用了.

Because both zoom() and subsample() want integer as parameters, I used both.

我不得不将 320x320 图像的大小调整为 250x250,结果是

I had to resize 320x320 image to 250x250, I ended up with

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)

这篇关于在 PhotoImage 下调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆