Python脚本检测损坏的图像 [英] Python Script to detect broken images

查看:579
本文介绍了Python脚本检测损坏的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个python脚本来检测损坏的图像并计数它们, 我的脚本中的问题是它检测到所有图像,但未检测到损坏的图像.如何解决这个问题.我指的是:

I wrote a python script to detect broken images and count them, The problem in my script is it detects all the images and does not detect broken images. How to fix this. I refered :

如何检查文件是否为有效的图片文件?输入我的代码

我的代码

import os
from os import listdir
from PIL import Image
count=0
for filename in os.listdir('/Users/ajinkyabobade/Desktop/2'):
    if filename.endswith('.JPG'):
     try:
      img=Image.open('/Users/ajinkyabobade/Desktop/2'+filename)
      img.verify()
     except(IOError,SyntaxError)as e:
         print('Bad file  :  '+filename)
         count=count+1
         print(count)

推荐答案

我在此处添加了另一个SO答案PIL解决方案可以更好地检测损坏的图像. 我还在我的Python脚本在GitHub上上实现了该解决方案.

I have added another SO answer here that extends the PIL solution to better detect broken images. I also implemented this solution in my Python script here on GitHub.

我还验证了损坏的文件(jpg)经常不是损坏"的图像,即,损坏的图片文件有时仍然是合法的图片文件,原始图像丢失或更改了,但是您仍然可以加载它.

I also verified that damaged files (jpg) frequently are not 'broken' images i.e, a damaged picture file sometimes remains a legit picture file, the original image is lost or altered but you are still able to load it.

为了完整起见,我引用了另一个答案:

I quote the other answer for completeness:

您可以使用具有大多数图像格式的Python Pillow (PIL)模块来检查文件是否为有效且完整的图像文件.

You can use Python Pillow(PIL) module, with most image formats, to check if a file is a valid and intact image file.

如果您还打算检测损坏的图像,则@Nadia Alramli会正确建议im.verify()方法,但是此不能检测所有可能的图像缺陷,例如,im.verify不会检测被截断的图像(大多数观看者通常会在其灰色区域加载该图像).

In the case you aim at detecting also broken images, @Nadia Alramli correctly suggests the im.verify() method, but this does not detect all the possible image defects, e.g., im.verify does not detect truncated images (that most viewer often load with a greyed area).

枕头也能够检测到此类缺陷,但是您必须在其中进行图像处理或图像解码/重新编码,或者触发检查.最后,我建议使用以下代码:

Pillow is able to detect these type of defects too, but you have to apply image manipulation or image decode/recode in or to trigger the check. Finally I suggest to use this code:

try:
  im = Image.load(filename)
  im.verify() #I perform also verify, don't know if he sees other types o defects
  im.close() #reload is necessary in my case
  im = Image.load(filename) 
  im.transpose(PIL.Image.FLIP_LEFT_RIGHT)
  im.close()
except: 
  #manage excetions here

在图像缺陷的情况下,此代码将引发异常. 请考虑im.verify大约比执行图像处理快100倍(我认为翻页是更便宜的转换之一). 使用此代码,您将以大约10 MBytes/sec(现代的2.5Ghz x86_64 CPU)验证一组图像.

In case of image defects this code will raise an exception. Please consider that im.verify is about 100 times faster than performing the image manipulation (and I think that flip is one of the cheaper transformations). With this code you are going to verify a set of images at about 10 MBytes/sec (modern 2.5Ghz x86_64 CPU).

对于其他格式 psd xcf ,..,您可以使用 Imagemagick 包装器 Wand ,代码如下:

For the other formats psd,xcf,.. you can use Imagemagick wrapper Wand, the code is as follows:

im = wand.image.Image(filename=filename)
temp = im.flip;
im.close()

但是,根据我的实验,Wand不能检测到截断的图像,我认为它会在没有提示的情况下将缺少的部分加载为灰色区域.

But, from my experiments Wand does not detect truncated images, I think it loads lacking parts as greyed area without prompting.

我认为 Imagemagick 具有一个外部命令 identify ,该命令可以完成任务,但是我没有找到调用该功能的方法以编程方式,我还没有测试过这条路线.

I red that Imagemagick has an external command identify that could make the job, but I have not found a way to invoke that function programmatically and I have not tested this route.

我建议始终执行初步检查,检查 filesize 不为零(或非常小),这是一个非常便宜的主意:

I suggest to always perform a preliminary check, check the filesize to not be zero (or very small), is a very cheap idea:

statfile = os.stat(filename)
filesize = statfile.st_size
if filesize == 0:
  #manage here the 'faulty image' case

这篇关于Python脚本检测损坏的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆