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

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


Hi,
So far I was just using
Image.open(filename)
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:
http://code.djangoproject.com/changeset/6175

==> apparently an older version of django also checked the
"OverflowError" exception in addition.
(comment:
# 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
django/trunk/django/core/validators.py:

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 ?

Thanks,
Sebastian Haase


More information about the Image-SIG mailing list