Detecting corrupted images in bash script [closed]

本小妞迷上赌 提交于 2019-12-20 09:00:12

问题


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


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/4780424/detecting-corrupted-images-in-bash-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!