Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Animation
#1
Hello, I have 4 pictures I want to create an animation from, but not sure how to do it. I´m already using the only library tkinter, so it shouldn´t use anything else. The names of my pictures are "screen1", "screen2", "screen3", "screen4". Can you give me any idea how to make it´s animation?

Thanks a lot!
Reply
#2
UP .....
Reply
#3
What do you mean by 'animation'? Is .gif an animation?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Yes, gif animation.
Reply
#5
One simple way is to use imageio:

>>> import imageio
>>> images = [imageio.imread(f'screen{i}.png') for i in range(1, 5)]
>>> imageio.mimsave('screen.gif', images, duration=1.0) 
  • imageio must be installed
  • files must be in the same directory where code is running
  • if files are in not in .png format change it on second row
  • writes screen.gif in same directory with each picture displayed with duration of 1.0 seconds (you can change to suit your needs; if omitted default is 0.5)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
Thank you, but as I mentioned above, it shouldn't require any other import than tkinter. Is there any other way?
Reply
#7
Here is a sample of my script:

    import tkinter
    from tkinter import *
    import random
    
    class Program:
    
        def __init__(self):
            self.canvas = tkinter.Canvas(width=1200, height=500, bg="black")
            self.canvas.pack()
            ...
    
        ...

    # some other class methods, not important for this
    # after one of those methods the part of my canvas changes color to grey and here should appear my animation, consisting of 4 images (win_screen1, win_screen2,...)
This animation should roll this pictures for few seconds, until clicking on a specific button. I mentioned my imports and basics of __init__() method to show, what should this animation use (so it shouldn´t require any other imports).

Here is what I tried:

 for i in range(100):
        for j in range(1,5):
            self.canvas.create_image(400, 200, image=win_screen{j})
Reply
#8
Start by just displaying the first one. Once that works, add a timer that rotates through other images quickly.

By default, tkinter's PhotoImage class (http://effbot.org/tkinterbook/photoimage.htm) only supports .gif or .pgm. If you want other formats, the official docs suggest installing the third party module pillow: https://pillow.readthedocs.io/en/5.3.x/

Once you have the image loaded, you can display it using a tk Label, using the image parameter.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020