getting size of gif

Fredrik Lundh fredrik at pythonware.com
Mon Mar 15 09:25:19 CET 2004


Garry Knight wrote:

> > note that you don't need all of PIL to run the above example; it's enough
> > to install Image.py, ImageFile.py, ImagePalette.py, and the
> > *ImagePlugin.py files you need for your application.  the binary
> > extensions are not needed.
>
> Gosh, what a payload!

you can download the files from the net; you don't have to type
them in yourself.

> It might not be the Python way of doing things, but this should work
> just as well:
>
> f = open('x.gif', 'rb')
> gif = f.read(6)         # 'gif89a'
> width = ord(f.read(1)) + 256 * ord(f.read(1))
> depth = ord(f.read(1)) + 256 * ord(f.read(1))
> print "width = %d, depth = %d" % (width, depth)
>
> You could just do f.seek(6) in the second line if you're not worried about
> checking that it's a valid gif file. Of course, this only works with
> single-image files, not with animated gifs.

it gives you the height, not the depth.  and it returns the size of
the logical screen, so it should work with animated GIFs as well.

but it doesn't give you any other information about the file, and it
doesn't work for JPEGs, PNG files, or any of the other 35+ formats
PIL supports.

</F>







More information about the Python-list mailing list