Play Animations in GIF with Tkinter [duplicate]
Asked Answered
B

2

23

I've been trying to play an animated gif using Tkinter.PhotoImage, but haven't been seeing any success. It displays the image, but not the animation. The following is my code:

root = Tkinter.Tk()
photo = Tkinter.PhotoImage(file = "path/to/image.gif")
label = Tkinter.Label(image = photo)
label.pack()
root.mainloop()

It displays the image in a window, and that's it. I'm thinking that the issue has something to do with Tkinter.Label but I'm not sure. I've looked for solutions but they all tell me to use PIL (Python Imaging Library), and it's something that I don't want to use.

With the answer, I created some more code (which still doesn't work...), here it is:

from Tkinter import *

def run_animation():
    while True:
        try:
            global photo
            global frame
            global label
            photo = PhotoImage(
                file = photo_path,
                format = "gif - {}".format(frame)
                )

            label.configure(image = nextframe)

            frame = frame + 1

        except Exception:
            frame = 1
            break

root = Tk()
photo_path = "/users/zinedine/downloads/091.gif"

photo = PhotoImage(
    file = photo_path,
    )
label = Label(
    image = photo
    )
animate = Button(
    root,
    text = "animate",
    command = run_animation
    )

label.pack()
animate.pack()

root.mainloop()

Thanks for everything! :)

Buchholz answered 14/2, 2015 at 17:13 Comment(7)
You could check if it has something to with being attached to a Label widget by using it instead on a Canvas widget (C.create_image(x, y, image=photo).Sleepyhead
I don't know if I'm doing something wrong with Canvas but I only get the bottom right corner of my image and it looks pixelated...Buchholz
Try getting it working with a non-animated image first, then switch to an animated one afterward.Sleepyhead
Same thing happens with non-animated gif...Buchholz
All I can say without seeing the code is that you must be doing it wrong.Sleepyhead
Another question here (that's mine): #28531915Buchholz
For what it's worth, here's example code showing how to put a gif image on a Canvas with Tkinter.Sleepyhead
K
21

You have to drive the animation yourself in Tk. An animated gif consists of a number of frames in a single file. Tk loads the first frame but you can specify different frames by passing an index parameter when creating the image. For example:

frame2 = PhotoImage(file=imagefilename, format="gif -index 2")

If you load up all the frames into separate PhotoImages and then use timer events to switch the frame being shown (label.configure(image=nextframe)). The delay on the timer lets you control the animation speed. There is nothing provided to give you the number of frames in the image other than it failing to create a frame once you exceed the frame count.

See the photo Tk manual page for the official word.

Kerri answered 14/2, 2015 at 18:54 Comment(4)
Sounds like a tKludge.Sleepyhead
And I should also ask if there is a way to determine the number of frames for an image; Tried this: #7504067 Also, for 2500 images, is there a way to ever make it automatic?Buchholz
See en.wikipedia.org/wiki/GIF#Animated_GIF for the layout. The GIF file does not contain a number of frames - only the delay per frame which may be variable.Kerri
Could you please provide a verificable example for this?Forayer
I
15

Here's a simpler example without creating an object:

from tkinter import *
import time
import os
root = Tk()

frameCnt = 12
frames = [PhotoImage(file='mygif.gif',format = 'gif -index %i' %(i)) for i in range(frameCnt)]

def update(ind):

    frame = frames[ind]
    ind += 1
    if ind == frameCnt:
        ind = 0
    label.configure(image=frame)
    root.after(100, update, ind)
label = Label(root)
label.pack()
root.after(0, update, 0)
root.mainloop()
Iritis answered 19/3, 2017 at 3:7 Comment(7)
Can you explain your code, so that others can learn from it instead of copy&pasting some piece of code that may or may not work?Male
It looks like it may work, but it kicks out a list index out of range for frame = frames[ind]Ablepsia
@Apostolos: It is because you tried to load 100 frames, but the gif that you used has less than 100 frames. Try reducing the 100 frames to 10 and gradually increase it until you hit an error.Goosy
I see. This is obviously the case. So, this time I used a GIF file with 32 frames and replaced 100 by 32. When all frames are displayed in the animation, I still get "IndexError: list index out of range". Anyway, I have created my own program that also counts the frames ... Moreover, it is also very smooth ... in the above example frame sizes change and the effect is very bad. I hope you have straightened this up.Valerie
A pretty interesting way, though it will work the right way only for GIFs with exactly 100 frames.Cherrylchersonese
@Valerie Care to share?Des
Share what? I can't find any solution I've given … Besides, this is 2 years old and I can don't even remember what's the issue!Valerie

© 2022 - 2024 — McMap. All rights reserved.