getting size of gif

David M. Wilson dw-google.com at botanicus.net
Wed Mar 10 00:57:10 CET 2004


biner.sebastien at ouranos.ca (biner) wrote...

>   I would like to know if there is a module out there to help me get
> the size of a gif image. The idea is to verify that the size is not
> greater than a certain value before forcing it toward a given
> dimension in a web page.

Try this:

    import struct

    def is_gif(in_file):
        hdr, ver, w, h = struct.unpack('3s3sHH', in_file.read(10))

        if hdr == 'GIF':
            return ver, w, h


It will return the GIF file version (87a or 89a), the width, and the
height. If the file is not a GIF, you will get None instead (or an
Exception if the file-like object you passed had less than 10 bytes
readable).

This will save you the use of a heavy duty library for the simple case
of extracting image dimensions.


>   Also, is there any way to know which function is used if we use two
> module that define the same function with the same name and arguments.
> E.g. the join function is present in the string module and the os.path
> module with string as input and output. Is there a way to force Python
> to tell me of the possible conflict?

If you have a 'join' function in some namespace and you don't know who
owns it, you should probably look at reorganising your code.

Alternatively:

    print "Is os.path.join:", join is os.path.join
    print "Is string.join:", join is string.join
    print "Is str.join:", join is str.join


David.



More information about the Python-list mailing list