Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Support for animated gifs in scene module?
-
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.
-
Animated gifs are not supported in scene, though webview supports them.
You could split the gif into images, and then animate them yourself. -
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. -
-
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?
-
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/
-
Here is the code to animate gif file in scene.
https://gist.github.com/balachandrana/c1580fff9477d58fd0cf25c898471597The frame rate may be low in slower machines. and the frame rate could be improved by caching.
-
-
@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.
-
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.
-
@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.gitIt 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.