用python(jpeg/png)或python图像验证pilg()

2024-10-01 10:12:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个大的图像数据集检测损坏的图像。我正在使用枕头包并验证()。 我只想检测损坏的图像或无法用图像查看器或浏览器打开的图像作为“坏苍蝇”,而我的所有图像总是检测为“坏的”

  • 我在一个git问题的评论中读到pillow只检测png文件,即使这样,我所有的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)

我做错什么了吗?在

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


Tags: 文件to数据from图像imageimportimg
1条回答
网友
1楼 · 发布于 2024-10-01 10:12:50

您需要在路径后添加/。否则您的完整路径看起来像.'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目录只包含图像,否则您将得到另一个错误。在

相关问题 更多 >