Skip to content

Instantly share code, notes, and snippets.

@balachandrana
Last active June 24, 2016 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balachandrana/c1580fff9477d58fd0cf25c898471597 to your computer and use it in GitHub Desktop.
Save balachandrana/c1580fff9477d58fd0cf25c898471597 to your computer and use it in GitHub Desktop.
# coding: utf-8
import scene
import ui
from PIL import Image as PILImage
class MyScene (scene.Scene):
def setup(self):
self.im = PILImage.open('tunnelswirl.gif')
self.mypalette = self.im.getpalette()
self.savefile = 'tmp.png'
self.toggle_state = False
self.sprite = scene.SpriteNode(scene.Texture(ui.Image.named('Snake')),
position=self.size/2,
parent=self)
def update(self):
if self.toggle_state:
try:
self.im.putpalette(self.mypalette)
new_im = PILImage.new("RGBA", self.im.size)
new_im.paste(self.im)
new_im.save(self.savefile)
self.sprite.texture = scene.Texture(ui.Image.named(self.savefile))
self.im.seek(self.im.tell()+1)
except EOFError:
self.im.seek(0)
#self.im.close()
#self.im = PILImage.open('tunnelswirl.gif')
#self.mypalette = self.im.getpalette()
def touch_began(self, touch):
self.toggle_state = not self.toggle_state
if not self.toggle_state:
self.sprite.texture = scene.Texture(ui.Image.named('Snake'))
scene.run(MyScene(), show_fps=True)
@cclauss
Copy link

cclauss commented Jun 24, 2016

On EOFError couldn't you just self.im.seek(0) instead of closing, reopening the image and resetting the palette?

An even slicker way to go might be to try to leverage ImageSequence.

In any case reducing "disk" IO from the animation process should speed things up a bit. I would have submitted a pull request but this code is in a gist, not in a repo.

@balachandrana
Copy link
Author

use seek(0) instead of opening and closing again as suggested by @ccc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment