This page outlines the processing of GIF files.
GIF files |
Theory
GIF files use LZW compression. For example let's take a simple 4x4 image with four blocks: Red; Blue; Green; and Black. The pixels could be:
R G B R R B G R R G B B G G R R
We read the colours from left to right. The first four colours are defined in the LZW table:
Entry Colour 0 Red 1 Blue 2 Green 3 Black 4 Red, Green 5 Green, Blue 6 Blue, Red 7 Red, Red 8 Red, Blue 9 Blue, Green 10 Green, Red 11 Red, Red, Green 12 Green, Blue, Blue 13 Blue, Green, Green 14 Green, Red, Red
Source code
The following outlines the Python code used:
import sys import os from PIL import Image file1 = "mini.gif" if (len(sys.argv)>1): file1=str(sys.argv[1]) im = Image.open(file1) if im.tile[0][0] == "gif": tag, (x0, y0, x1, y1), offset, extra = im.tile[0] im.size = x1-x0, y1-y0 im.tile = [(tag, (0, 0) + im.size, offset, extra)] print "X0:\t",x0 print "Y0:\t",y0 print "X1:\t",x1 print "Y1:\t",y1 print "Offset:\t",offset print "Extra:\t",extra print "Image size:\t",im.size print "Image title:\t",im.tile nframes = 0 while im: # im.save( '%s/%s-%s.gif' % ('out', os.path.basename('out'), nframes ) , 'GIF') nframes += 1 try: im.seek( nframes ) except EOFError: break; print "No of frames:\t",nframes