【问题标题】:images (png, jpg or jpeg) is detected as corrupt with python's PIL / Pillow verify()图像(png、jpg 或 jpeg)被 python 的 PIL / Pillow verify() 检测为损坏
【发布时间】:2019-12-31 14:43:29
【问题描述】:

我正在尝试从大型图像数据集中检测损坏的图像。我正在使用 Pillow 包和验证()。 我只是想将已损坏或无法使用图像查看器或浏览器打开的图像检测为“坏蝇”,而我的所有图像总是被检测为“坏”

  • 我在 git 问题评论中读到,pillow 只检测 png 文件,即使这样,我的所有 png 图像都被检测为坏的。
  • 我还使用在线随机下载的图像测试了代码,所有这些都被检测为“坏文件”
  • 注意:我在 notebook++ 中编码(这应该不是问题,对吧?)
from os import listdir
from PIL import Image

for imageFolder in listdir('./batch1'):
    try:
        img = Image.open('./batch1'+imageFolder)
        img.verify()     # to veify if its an img
        img.close()     #to close img and free memory space
    except (IOError, SyntaxError) as e:
        print('Bad file:', imageFolder)

我做错了吗?

有没有其他方法可以实现检测损坏图像而不手动删除每个损坏图像的目标?

【问题讨论】:

  • 您需要多考虑一下什么是文件、什么是目录以及如何将两者结合起来。尝试先形成文件的全名,然后使用img=Image.open(fullname)
  • @MarkSetchell 哦……谢谢 ;)
  • 您可以很容易地检测截断... JPEG 必须有 EOI (\xff \xd9) 作为其最后 2 个字节 en.m.wikipedia.org/wiki/JPEG_File_Interchange_Format 并且 PNG 必须在末尾有 IEND 块...en.m.wikipedia.org/wiki/Portable_Network_Graphics
  • 再次感谢@Mark .....我查看了标记的链接,我明白了....但我正在寻找库(最好在python中)或检测损坏图像的方法自动使用代码
  • ImageMagick 可以检测到此类错误...stackoverflow.com/a/46805566/2836621 所以我认为 Python Wand 可以,所以也许 @emcconville 可以提供帮助

标签: python image python-imaging-library


【解决方案1】:

您需要在路径后添加/。否则你的完整路径看起来像.'batch1your_img.png

from os import listdir
from PIL import Image

for imageFolder in listdir('./batch1'):
    try:
        img = Image.open('./batch1/'+imageFolder)
        img.verify()     # to veify if its an img
        img.close()     #to close img and free memory space
    except (IOError, SyntaxError) as e:
        print('Bad file:', imageFolder)

还要确保您的 batch1 目录仅包含图像,否则您会收到另一个错误。

【讨论】:

  • 我的天哪!谢谢!!! .....我完全没有注意到这一点。谢谢......解决了将所有图像检测为损坏的问题,但是,我仍然无法检测到实际损坏的图像(大约 68 字节的图像和图像查看器应用程序无法打开)......再次感谢!
猜你喜欢
  • 1970-01-01
  • 2018-03-29
  • 1970-01-01
  • 2012-04-13
  • 1970-01-01
  • 1970-01-01
  • 2014-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多