在Tkinter中显示时,PIL会扭曲动画GIF的质量

时间:2019-03-19 15:26:39

标签: python-3.x tkinter python-imaging-library animated-gif

我的代码有问题,并且使用PIL library相对较新。我的代码可以工作,但是GIF的质量非常差,我不确定如何解决它。我的代码如下:

import tkinter
from tkinter import *
from PIL import Image, ImageTk

window = tkinter.Tk()
window.title("****** Interface")
window.configure(background="white")

# Photo
class MyLabel(tkinter.Label):
    def __init__(self, master, filename):
        im = Image.open(filename)
        seq =  []
        try:
            while 1:
                seq.append(im.resize((480, 270)).copy())
                im.seek(len(seq)) # skip to next frame
        except EOFError:
            pass

        try:
            self.delay = im.info['duration']

        except KeyError:
            self.delay = 45

        first = seq[0].convert('RGB')
        self.frames = [ImageTk.PhotoImage(first)]

        tkinter.Label.__init__(self, master, image=self.frames[0])

        temp = seq[0].convert('RGB')
        for image in seq[1:]:
            frame = image.convert('RGB')
            temp.paste(frame)
            self.frames.append(ImageTk.PhotoImage(temp))

        self.idx = 0

        self.cancel = self.after(self.delay, self.play)

    def play(self):
        self.config(image=self.frames[self.idx])
        self.idx += 1
        if self.idx == len(self.frames):
            self.idx = 0
        self.cancel = self.after(self.delay, self.play)        


lbl = MyLabel(window, '*****.gif')
lbl.pack(anchor="center")
window.mainloop()

我看了链接Displaying animated GIFs in Tkinter using PIL上提供的解决方案,但是无法使它们起作用。请协助。

0 个答案:

没有答案