gpt4 book ai didi

python - 在 Tkinter 的 PIL 中调整图片大小

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

我目前正在使用 PIL 在 Tkinter 中显示图像。我想暂时调整这些图片的大小,以便更容易查看。我该怎么做?

片段:

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)

最佳答案

这是我所做的并且效果很好...

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

这是我从以下示例改编的完整工作代码:

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

注意...在检查了 ImageTK 的 PhotoImage 类的 pythonware 文档(非常稀疏)之后,我发现如果您的代码片段有效,那么只要您导入 PIL“Image”Library 和 PIL“ImageTK”Library 以及 PIL 和tkinter 是最新的。另一方面,我什至找不到我一生的“ImageTK”模块。你能发布你的进口商品吗?

关于python - 在 Tkinter 的 PIL 中调整图片大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4066202/

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