我的代码有问题,并且使用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上提供的解决方案,但是无法使它们起作用。请协助。