在 bash 脚本中检测损坏的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4780424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:19:47  来源:igfitidea点击:

Detecting corrupted images in bash script

bashimage-processingpng

提问by skazhy

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.

我有超过 2000 张来自网络摄像头流的图像(对于延时视频),我需要删除所有不完整和损坏的图像,然后再将它们传递给 php-gd 脚本,该脚本为最终视频编辑它们。

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 Errorin console

是否可以使用 imagemagick 或其他一些工具检测损坏的文件?如果我尝试使用 feh 打开损坏的图像,它会显示libpng error: Read Error在控制台中

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

更新:在我的情况下,建议的识别方法似乎接受坏图像。这是一个损坏的示例http://imgur.com/YcB9n

采纳答案by dogbane

Try ImageMagick's identifycommand. From the man page:

试试 ImageMagick 的identify命令。从手册页:

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):

另一种方法是使用PIL(Python 成像库)

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.

验证()

尝试确定文件是否损坏,而不实际解码图像数据。如果此方法发现任何问题,则会引发适当的异常。此方法仅适用于新打开的图像;如果图像已加载,则结果未定义。另外,如果使用此方法后需要加载图像,则必须重新打开图像文件。

回答by sarnold

I tried the ImageMagick identifycommand 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:

identify在一个 jpg 上尝试了 ImageMagick命令,我已经放置了几种类型的损坏。它能够识别一些,但不是全部,所以这充其量只是部分解决方案,但试试这个:

for f in *.JPG ; do identify $f > /dev/null || echo $f >> /tmp/fail ; done ; cat /tmp/fail