ImageMagick to verify image integrity

Kiarash picture Kiarash · Jul 20, 2013 · Viewed 9k times · Source

I'm using ImageMagick (with Wand in Python) to convert images and to get thumbnails from them. However, I noticed that I need to verify whether a file is an image or not ahead of time. Should I do this with Identify?

So I would assume checking the integrity of a file needs the whole file to be read into memory. Is is better to try and convert the file and if there was an error, then we know the file wasn't good.

Answer

jsp picture jsp · Jul 20, 2013

seems like you answered your own question

$ ls -l *.png
-rw-r--r-- 1 jsp jsp 526254 Jul 20 12:10 image.png
-rw-r--r-- 1 jsp jsp  10000 Jul 20 12:12 image_with_error.png
$ identify image.png &> /dev/null; echo $?
0
$ identify image_with_error.png &> /dev/null; echo $?
0
$ convert image.png /dev/null &> /dev/null ; echo $?
0
$ convert image_with_error.png /dev/null &> /dev/null ; echo $?
1