1

I am having an issue with my code and am relatively new to using the PIL library. My code works, however the GIF is extremely bad quality and i'm not exactly sure how to fix it. My code is below:

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

I have taken a look at the solution provided at the link Displaying animated GIFs in Tkinter using PIL however I could not get them to work. Please assist.

Kusi
  • 785
  • 1
  • 10
  • 21
  • Have you tried the answer [here](https://stackoverflow.com/questions/43770847/play-an-animated-gif-in-python-with-tkinter)? Worked pretty well for me. – Henry Yik Mar 20 '19 at 06:55
  • That was actually the first answer I tried out, I still facing the same issue. Please help. – Kusi Mar 20 '19 at 10:32
  • 1
    I figured it out, it was the quality of the gif not the actual code...the gif needs to be unoptimised. – Kusi Mar 22 '19 at 08:37

0 Answers0