使用 Tkinter/Python 使用 ImageTk.PhotoImage 调整图像大小 [英] Resizing images with ImageTk.PhotoImage with Tkinter/Python

查看:233
本文介绍了使用 Tkinter/Python 使用 ImageTk.PhotoImage 调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Tkinter 制作幻灯片,但无法调整图像大小.它们仅显示为默认大小,而我想让它们全部统一.我可以使用 Image.open 和调整大小对单个图像执行此操作,但我无法弄清楚如何让它在迭代中工作.感谢您的帮助:

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

基本上,在创建 ImageTk.PhotoImage 实例之前,必须使用 PIL 图像来调整图像大小.我在示例中对两个关键点进行了注释,因此您知道在哪里查找.希望这会有所帮助.

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.

这篇关于使用 Tkinter/Python 使用 ImageTk.PhotoImage 调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆