我目前正在使用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)
答案 0 :(得分:26)
这是我做的,而且效果很好......
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”库和PIL“ImageTK”库以及PIL和tkinter是最新的。另一方面,我甚至无法找到“ImageTK”模块的生命。你能发布你的进口吗?
答案 1 :(得分:5)
如果您不想保存,可以试试:
from Tkinter import *
from PIL import Image, ImageTk
root = Tk()
same = True
#n can't be zero, recommend 0.25-4
n=2
path = "../img/Stalin.jpeg"
image = Image.open(path)
[imageSizeWidth, imageSizeHeight] = image.size
newImageSizeWidth = int(imageSizeWidth*n)
if same:
newImageSizeHeight = int(imageSizeHeight*n)
else:
newImageSizeHeight = int(imageSizeHeight/n)
image = image.resize((newImageSizeWidth, newImageSizeHeight), Image.ANTIALIAS)
img = ImageTk.PhotoImage(image)
Canvas1 = Canvas(root)
Canvas1.create_image(newImageSizeWidth/2,newImageSizeHeight/2,image = img)
Canvas1.config(bg="blue",width = newImageSizeWidth, height = newImageSizeHeight)
Canvas1.pack(side=LEFT,expand=True,fill=BOTH)
root.mainloop()
答案 2 :(得分:1)
最简单的方法可能是根据原件创建新图像,然后用较大的副本换出原件。为此,tk图像具有copy
方法,可以在制作副本时对原始图像进行缩放或子采样。不幸的是,它只允许你以2的因子进行缩放/子采样。