在 Tkinter 中调整 PIL 中的图片大小 [英] Resizing pictures in PIL in Tkinter

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

问题描述

我目前正在使用 PIL 在 Tkinter 中显示图像.我想暂时调整这些图像的大小,以便可以更轻松地查看它们.我该怎么办?

I'm currently using PIL to display images in Tkinter. I'd like to temporarily resize these images so that they can be viewed more easily. How can I go about this?

片段:

self.pw.pic = ImageTk.PhotoImage(Image.open(self.pic_file))
self.pw.pic_label = TK.Label(self.pw , image=self.pw.pic,borderwidth=0)         
self.pw.pic_label.grid(column=0,row=0)

推荐答案

这就是我所做的,而且效果很好...

Here's what I do and it works pretty well...

image = Image.open(Image_Location)
image = image.resize((250, 250), Image.ANTIALIAS) ## The (250, 250) is (height, width)
self.pw.pic = ImageTk.PhotoImage(image)

你去:)

这是我的导入语句:

from Tkinter import *
import tkFont
from PIL import Image

这是我改编自此示例的完整工作代码:

And here is the complete working code I adapted this example from:

im_temp = Image.open(Image_Location)
im_temp = im_temp.resize((250, 250), Image.ANTIALIAS)
im_temp.save("ArtWrk.ppm", "ppm") ## The only reason I included this was to convert
## The image into a format that Tkinter woulden't complain about
self.photo = PhotoImage(file="ArtWrk.ppm") ## Open the image as a tkinter.PhotoImage class()
self.Artwork.destroy() ## Erase the last drawn picture (in the program the picture I used was changing)
self.Artwork = Label(self.frame, image=self.photo) ## Sets the image too the label
self.Artwork.photo = self.photo ## Make the image actually display (If I don't include this it won't display an image)
self.Artwork.pack() ## Repack the image

这里是 PhotoImage 类文档:http://www.pythonware.com/library/tkinter/introduction/photoimage.htm

And here are the PhotoImage class docs: http://www.pythonware.com/library/tkinter/introduction/photoimage.htm

注意...在检查了 ImageTK 的 PhotoImage 类(非常稀疏)上的 pythonware 文档后,我似乎认为,如果您的代码段有效,那么只要您导入 PILImage"库和 PILImageTK"库,PIL 和tkinter 是最新的.另一方面,我什至找不到适合我生活的ImageTK"模块生活.你能发布你的导入吗?

Note... After checking the pythonware documentation on ImageTK's PhotoImage class (Which is very sparse) I appears that if your snippet works than this should as well as long as you import the PIL "Image" Library an the PIL "ImageTK" Library and that both PIL and tkinter are up-to-date. On another side-note I can't even find the "ImageTK" module life for the life of me. Could you post your imports?

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

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