2015-02-11 179 views
1

我正在試圖用Tkinter製作一個幻燈片,但我在調整圖像時遇到了問題。他們只顯示爲默認大小,而我想使他們都統一。我可以使用Image.open調整個別圖像並調整大小,但我無法理清如何讓它在迭代中工作。我會很感激的幫助:用Tkinter/Python調整ImageTk.PhotoImage圖像大小

import Tkinter as tk 
from PIL import Image, ImageTk 
from itertools import cycle 

class App(tk.Tk): 
    def __init__(self, image_files, x, y, delay): 
     tk.Tk.__init__(self) 
     self.geometry('+{}+{}'.format(x,y)) 
     self.delay = delay 
     self.pictures = cycle((ImageTk.PhotoImage(file=image), image) for image in image_files) 
     self.pictures = self.pictures 
     self.picture_display = tk.Label(self) 
     self.picture_display.pack() 
    def show_slides(self): 
     img_object, img_name = next(self.pictures) 
     self.picture_display.config(image=img_object) 
     self.title(img_name) 
     self.after(self.delay, self.show_slides) 
    def run(self): 
     self.mainloop() 
delay = 3500 

image_files = [ 
'c:/users/xxx/pictures/47487_10100692997065139_1074926086_n.jpg', 
'E:\\1415\\20141216_105336.jpg' 
] 

x = 100 
y = 50 
app = App(image_files,x,y,delay) 
app.show_slides() 
app.run() 
+0

你試過myImage.subsample(N,N)嗎? – jmercier 2015-02-11 23:09:07

回答

2

你接近,但還沒有應用。因此,我改變了你的榜樣,使其工作:

import Tkinter as tk 
from PIL import Image, ImageTk 
from itertools import cycle 

class App(tk.Tk): 

    def __init__(self, image_files, x, y, delay): 
     tk.Tk.__init__(self) 
     self.geometry('+{}+{}'.format(x,y)) 
     self.delay = delay 
     #self.pictures = cycle((ImageTk.PhotoImage(file=image), image) for image in image_files) 
     self.pictures = cycle(image for image in image_files)   
     self.pictures = self.pictures 
     self.picture_display = tk.Label(self) 
     self.picture_display.pack() 
     self.images = [] # to keep references to images. 

    def show_slides(self):   
     img_name = next(self.pictures) 
     image_pil = Image.open(img_name).resize((300, 300)) #<-- resize images here 

     self.images.append(ImageTk.PhotoImage(image_pil))  

     self.picture_display.config(image=self.images[-1]) 
     self.title(img_name) 
     self.after(self.delay, self.show_slides) 

    def run(self): 
     self.mainloop() 

delay = 3500 

image_files = [ 
      './empty.gif', 
      './empty2.gif', 
      './empty1.gif' 
     ] 

x = 200 
y = 150 
app = App(image_files,x,y,delay) 
app.show_slides() 
app.run() 

基本上,圖片大小必須使用PIL圖像來完成,你做的ImageTk.PhotoImage實例前。在兩個關鍵點上,我在示例中提出了評論,所以您知道在哪裏尋找。希望這可以幫助。

+0

這比我想出來的方式更簡單=) – 2015-02-13 15:46:20