Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting corrupted images in bash script [closed]

I have >2000 images from a webcam stream (for a time-lapse video), I need to delete all incomplete & corrupted images, before passing them to a php-gd script that edits them for the final video.

Is it possible to detect corrupted files with imagemagick or some other tool? If i try to open the corrupted image with feh it displays libpng error: Read Error in console

Thanks in advance!

UPDATE: It seems that the suggested identify method accepts the bad images in my case. Here is an example of a corrupted one http://imgur.com/YcB9n

like image 491
skazhy Avatar asked Jan 24 '11 09:01

skazhy


2 Answers

Try ImageMagick's identify command. From the man page:

Identify describes the format and characteristics of one or more image files. It will also report if an image is incomplete or corrupt.

Example:

$ identify foo.png identify: NotAPNGImageFile (foo.png).  $ echo $? 1 

An alternative, is to use PIL (Python Imaging Library):

from PIL import Image  im = Image.open("foo.png") im.verify() 

From the documentation:

im.verify()

Attempts to determine if the file is broken, without actually decoding the image data. If this method finds any problems, it raises suitable exceptions. This method only works on a newly opened image; if the image has already been loaded, the result is undefined. Also, if you need to load the image after using this method, you must reopen the image file.

like image 187
dogbane Avatar answered Oct 16 '22 22:10

dogbane


I tried the ImageMagick identify command on a jpg I had laying around with several kinds of corruptions thrown in. It was able to identify some, but not all, so this might just be a partial solution at best, but try this:

for f in *.JPG ; do identify $f > /dev/null || echo $f >> /tmp/fail ; done ; cat /tmp/fail
like image 42
sarnold Avatar answered Oct 16 '22 23:10

sarnold