[Image-SIG] how to verify image file format - difficult !

Sebastian Haase seb.haase at gmail.com
Mon Nov 5 13:52:55 CET 2007

So far I was just using
to determine if a file was an image file or not.
I thought a simple "except IOError" would catch non-image files.

Now, I suddently got a SystemError (message: "tile cannot extend
outside image") instead.
A google-search brought up the "ImageValidator" function from the
"django" project:

==> apparently an older version of django also checked the
"OverflowError" exception in addition.
# OverflowError is due to a bug in PIL with Python 2.4+ which can cause
# it to gag on OLE files.

and the currently django file contains this code-snipplet in

184         # load() is the only method that can spot a truncated JPEG,
185         #  but it cannot be called sanely after verify()
186         trial_image = Image.open(StringIO(content))
187         trial_image.load()
188         # verify() is the only method that can spot a corrupt PNG,
189         #  but it must be called immediately after the constructor
190         trial_image = Image.open(StringIO(content))
191         trial_image.verify()

Are these obscure constrains ("load() cannot be called sanely after
verify()" ) still needed ?
And is it really suggested to "load" the entire image first, just to
tell if it's valid ?

Sebastian Haase

More information about the Image-SIG mailing list