[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Animated GIFs



Hi,

On Dec 8, 2007 10:00 AM, Ian Mallett <geometrian@xxxxxxxxx> wrote:
> Like for alpha channels?  Try saving .png s in a folder.  I don't
> think there's a way to do it with PIL in one file.
>

You could always make a GIMP file plugin that takes an image with a
frame per layer (as you get when loading an animated GIF; GIMP can
easily replay this 'format' once it's loaded, even with 24bit
images.), and saves it as an uncompressed zip containing one PNG per
frame, with filenames like '000.png','001.png','002.png'.
You might want to attach timing information as well.
(and possibly write code to also do the reverse -- create a single
gimp image from your zipped animation format.)

With the zipfile module, loading the animation is then pretty simple.

>>> import zipfile
>>> animfname = '/tmp/test.zip'
>>> z = zipfile.Zipfile (animfname,'r')
>>> import cStringIO as SIO
>>> import pygame.image
>>> frames = []
>>> for item in z.infolist ():
...     tmpfile = SIO.StringIO (z.read (item.name))
...     thisframe = pygame.image.load (tmpfile)
...     tmpfile.close ()
...     frames.append (thisframe)

>>> z.close()

-- I'd like to know if anyone has found a way to feed the filehandle
returned from z.open() into pygame.image.load(). My attempts lead to
"error: Can't seek in this data source"!