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

[pygame] Animated Gifs again :)



Hello. I have written this piece of code after some people here told me I have to use the PIL library to make animated GIFs animate:
class GifImage: #class which should help with animating GIFs
  def __init__(self,fname):
    self.frames=[]
    self.index=0
    im = Image.open(fname)
    try:
      while 1:
        i=im.convert("RGBX")# I decided to use this because in mode "P" pygame returns only black screen
        self.frames+=[image.fromstring(i.tostring(),i.size,"RGBX").convert_alpha()] #Adds to GifImage instane's frame list surface
        im.seek(im.tell()+1) # jumps to the next frame
    except EOFError: #when there's no frame left
      pass # end of sequence
  def surf(self,time): #returns frame at int(self.index) and adds time to self.index
    a=self.frames[int(self.index)]
    self.index+=time
    if self.index>len(self.frames):self.index=0 #if this is the last frame, start from the beginning
    return a

But it doesn't return image how I want, but it returns a strange-colored surface. Can somebody help or improve my code?
Thank you very much ;)