So I saw this code on YouTube, and wanted to try it, since I need animated gif in my program, I made a throwaway file to test if it works, and it works, but only on downloaded gifs. Though, I don't need downloaded one, but the one I made. I have added the gif that work as an attachment, but mine is a few KB bigger than the limit(https://i.sstatic.net/Sfv7d.gif).
When I added my gif in the file=".gif" line, and program was run, only the console appeared, and root wasn't visible, but with the other one, everything worked as how it should have. If it is of any help, my gif is quite large dimension wise(1600x900px).
Here is the code:
import tkinter as tk
from PIL import Image
root = tk.Tk()
file="star.gif"
info = Image.open(file)
frames = info.n_frames # gives total number of frames that gif contains
# creating list of PhotoImage objects for each frames
im = [tk.PhotoImage(file=file,format=f"gif -index {i}") for i in range(frames)]
count = 0
anim = None
def animation(count):
global anim
im2 = im[count]
gif_label.configure(image=im2)
count += 1
if count == frames:
count = 0
anim = root.after(50,lambda :animation(count))
def stop_animation():
root.after_cancel(anim)
gif_label = tk.Label(root,image="")
gif_label.pack()
start = tk.Button(root,text="start",command=lambda :animation(count))
start.pack()
stop = tk.Button(root,text="stop",command=stop_animation)
stop.pack()
root.mainloop()
Any help would be great, and thank you in advance!!
Here is the gif that won't work: Gif that doesnt work
The provided not working GIF actually works, but it takes time to load all the frames. It takes around 70 seconds in my PC with AMD Ryzen 9 CPU.
It is a lot faster if using Pillow
module:
...
from PIL import Image, ImageTk, ImageSequence
...
im = [ImageTk.PhotoImage(img) for img in ImageSequence.Iterator(info)]
...
It takes only around 0.2 seconds.