Detecting corrupted images in bash script [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this questionI 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
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.
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
精彩评论