Forum Archive

Support for animated gifs in scene module?

Kamozo

Hi, I was wondering if the scene module supported gifs. I've tried using them in my own project but they are just a still image.
Here's some example code so you can see what I'm trying to do:

from scene import *
class MyScene (Scene):
    def setup(self):
        self.background_color = 'midnightblue'
        self.ship = SpriteNode('ship.GIF') #Why won't it play?
        self.ship.position = self.size / 2
        self.add_child(self.ship)
run(MyScene())

Help would be appreciated, thanks.

JonB

Animated gifs are not supported in scene, though webview supports them.
You could split the gif into images, and then animate them yourself.

Kamozo

Oh, okay. It'd be cool if they were. Perhaps maybe in a future version.
I've tried splitting them, but I don't know how to make it continuously repeat.

ccc

https://forum.omz-software.com/topic/2052/gif-art-in-python

Kamozo

That's useful, but what if I wanted to animate a gif using the scene module only, with my own images? Would that be possible?

brumm

The scene draw method is called up to 60 times per second. So your code should not need more than 1/60s or you reduce the interval. However first you should read your images to the memory. Last tip https://github.com/humberry/scene-tutorial/

abcabc

Here is the code to animate gif file in scene.
https://gist.github.com/balachandrana/c1580fff9477d58fd0cf25c898471597

The frame rate may be low in slower machines. and the frame rate could be improved by caching.

ccc

@abcabc Great stuff! Comments on the gist.

abcabc

@ccc Thanks for the suggestions. As suggested by you, I updated the code to use seek(0) instead of closing and opening files. It is a good suggestion although it did not improve the speed much. I will implement the caching and will also put the code in github so that you can do the pull request. I will also use the imagesequence rather than seek and tell.

JonB

I would think I would be better to pre-load the images into a list of ui.Images, that way you don't waste time converting. Also, BytesIO works well for saving data to avoid disk usage. Image.from_data reads from a bytesio.

abcabc

@JonB Thanks for the suggestions. Created a GifSpriteNode class which could be used like SpriteNode. Added support for duration. Used BytesIO instead of files. Added preload option. Here is the git repository
https://github.com/balachandrana/animating_gif_in_pythonista_scene.git

It does not seem to work for all gifs. There is no straight-forward PIL API to take care of all situations. Tried some solutions suggested on stackoverflow and other web sites but still all gifs do not work. Anyway I will try to see what could be done.