简体   繁体   中英

Resizing images with ImageTk.PhotoImage with Tkinter/Python

I'm attempting to make a slideshow with Tkinter but I'm having trouble sizing the images. They only show as the default size while I'd like to make them all uniform. I can do it for individual images using Image.open and resize, but I can't sort out how to get it to work over an iteration. I'd appreciate the help:

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

You were close, but not quite there yet. Thus, I changed your example to make it work:

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

Basicly, image resizing must be done using PIL image, before you make an instance of ImageTk.PhotoImage . In two critical points I made comments in the example, so you know where to look for. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM