2010-11-01 92 views
10

我目前使用PIL在Tkinter中显示图片。我想暂时调整这些图像的大小,以便更容易地查看它们。我怎么去解决这个问题?在Tkinter PIL中调整图片大小

段:

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) 

回答

20

这里是我做的,它工作得很好......

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 
import Image #This is the PIL Image library 

这里是完整的工作代码,我适应从这个例子:

im_temp = Image.open(Path-To-Photo) 
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 dont include this it won't display an image) 
self.Artwork.pack() ##repack the image 

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

注... 检查上ImageTK的光象的pythonware文档后类(这是非常稀疏的)我看来,如果你的代码片段工作得比这更好,只要你导入PIL“Image”库和PIL“ImageTK”库并且PIL和tkinter都是最新的。另一方面,我甚至无法在我的生活中找到“ImageTK”模块的使用寿命。你可以发布你的进口?

+2

我不断收到这个“AttributeError的:光象实例没有属性“调整“”。我需要导入什么? – rectangletangle 2010-11-01 02:26:34

+0

@ Anteater7171包含更多信息 – Joshkunz 2010-11-01 23:40:10

+0

它是(宽度,高度),而不是(高度,宽度)。 – Jacob 2018-02-27 20:08:35

1

最简单的可能是基于原件创建新图像,然后用较大的副本换出原件。为此,tk图像有一个copy方法,可以让您在制作副本时缩放或二次采样原始图像。不幸的是,只有让你放大/子样本中的2

3

因素,如果你不想保存它,你可以尝试一下:

from Tkinter import * 
import ImageTk 
import Image 

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()