This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On
EOFError
couldn't you justself.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.