2010-11-01 123 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()