Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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

This comment has been minimized.

Copy link

@cclauss 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

This comment has been minimized.

Copy link
Owner Author

@balachandrana balachandrana commented Jun 24, 2016

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