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

Re: [pygame] about animation gif file



Terry Hancock schrieb:
> On Saturday 24 September 2005 03:17 am, Kris Schnee wrote:
> 
>>flya flya wrote:
>>
>>>some gif file has many frames to paly animation,Can pygame load these
>>>frames? and do the animation?
>>
>>Yes. Load the GIF as a Surface. Make a bunch of Rect objects 
>>representing where in the image each frame is. Use those as clipping 
>>regions.
> 
> 
> Are you saying that the loader will automatically load the animated
> GIF by separating each frame out spatially on the Surface? If so, it
> has to be pretty smart, and these aren't sufficient instructions for
> figuring out what it does.
> 
> We're talking about a multi-frame format in the GIF, not frames spread
> out on a static GIF.  There is no such thing as "where in the image
> each frame is" -- they are in the same place, they are separated by
> *time* (i.e. frame number), and therefore by where they are stored in
> the file, but there's no such thing as a separate clipping rectangle
> for each frame as you seem to be imagining.
> 
> Most single-frame image tools only load the *first* frame of an animated
> GIF and ignore the rest.  I would naively suspect that this is what will
> happen if you try to load one with PIL (which I think is what you must
> do to load any image into PyGame unless it's in Microsoft BMP format --
> which I must say seems like a strange choice of native format ;-) ),
> but I haven't tried it myself, so I'm not sure.
> 
> You can do some trickery on the command line to change an animated GIF
> to the kind of format you are thinking of, using ImageMagick, though
> I'd have to spend some time with the man pages to figure out how. You
> might be able to do the same with PIL, though I have doubts, since PIL
> doesn't provide all that much support for GIF.

Attached is an example script that loads an animated gif from the filename
given on the command line and turns it's frames into a list of pygame surfaces.

You can then use this list for creating an animated sprite (look in the pygame
code repository for how to do that)

HTH, Chris

P.S. I don't know, if this code will handle transparency properly. Probably you
have to set it on each surface manually.
import Image
import pygame
OUTPUT_FORMAT='png'

def load_gif_anim(file, mode="P"):
    img = Image.open(file)
    l = []; i = 0
    while True:
        try:
            img.seek(i)
        except EOFError:
            break
        pg_surf = pygame.image.frombuffer(img.tostring("raw", mode),
          img.size, mode)
        l.append(pg_surf)
        i += 1
    return l

if __name__ == '__main__':
    import sys
    print load_gif_anim(sys.argv[1])

Attachment: signature.asc
Description: OpenPGP digital signature