Determining if file is valid image file

Jarek Zgoda jzgoda at o2.usun.pl
Thu Aug 2 17:20:36 EDT 2007


Terry Reedy napisał(a):

>>>>> Other than installing PIL, is there a "simple" way using Python only
>>>>> to determine if a file is a valid image file?
> [...]
>> Be aware that broken images (i.e. partially downloaded) in many cases
>> pass the imghdr.what() test.
> 
> To put it another way, the only way to determine whether a coded file is 
> valid may be to decode it.  And even then, it may be corrupted in the sense 
> that the decoded version may have artifacts not in the original.  I have 
> seen the latter both in jpeg images and movie DVDs.

That's what I mean, images that cann't be read using PIL sometimes are
recognized by imghdr.what(), as it happens with "file" too - both of
these tools are the identification (not validation) utilities. To be
sure the image is "really valid", you have to use some image
manipulation program (or library), like ImageMagick (or PIL). Sometimes
imghdr.what() is enough, sometimes you need more. ;)

-- 
Jarek Zgoda
http://jpa.berlios.de/



More information about the Python-list mailing list