def readGif(filename, asNumpy=True):
 if (PIL is None):
  raise RuntimeError('Need PIL to read animated gif files.')
 if (np is None):
  raise RuntimeError('Need Numpy to read animated gif files.')
 if (not os.path.isfile(filename)):
  raise IOError(('File not found: ' + str(filename)))
 pilIm = PIL.Image.open(filename)
 pilIm.seek(0)
 images = []
 try:
  while True:
   tmp = pilIm.convert()
   a = np.asarray(tmp)
   if (len(a.shape) == 0):
    raise MemoryError('Too little memory to convert PIL image to array')
   images.append(a)
   pilIm.seek((pilIm.tell() + 1))
 except EOFError:
  pass
 if (not asNumpy):
  images2 = images
  images = []
  for im in images2:
   images.append(PIL.Image.fromarray(im))
 return images

def readGif ( filename , asNumpy = True ) : if ( PIL is None ) : raise RuntimeError ( 'Need PIL to read animated gif files.' ) if ( np is None ) : raise RuntimeError ( 'Need Numpy to read animated gif files.' ) if ( not os . path . isfile ( filename ) ) : raise IOError ( ( 'File not found: ' + str ( filename ) ) ) pilIm = PIL . Image . open ( filename ) pilIm . seek ( 0 ) images = [ ] try : while True : tmp = pilIm . convert ( ) a = np . asarray ( tmp ) if ( len ( a . shape ) == 0 ) : raise MemoryError ( 'Too little memory to convert PIL image to array' ) images . append ( a ) pilIm . seek ( ( pilIm . tell ( ) + 1 ) ) except EOFError : pass if ( not asNumpy ) : images2 = images images = [ ] for im in images2 : images . append ( PIL . Image . fromarray ( im ) ) return images
readgif read images from an animated gif file .