Python: 检测指定目录下图片是否存在问题

当我们使用opencv读取图片或者使用Deep learning读取数据集时,如果图片有问题则会造成错误!

如何检测一个文件夹内所有图片是否都没有错误呢?这里总结以下代码。

参考链接:https://www.computationalimaging.cn/2020/01/python-detect-whether-there-is-problem.html

#! /usr/bin/env python3
# -*- coding:utf-8 -*- 
import cv2
import os

path = 'F://chaomo/'
filelist = os.listdir(path)
count = 0
sum_len = len(filelist)
for item in filelist:
    image = cv2.imread(path+item)
    # print(item)
    try:
        cv2.imshow('img',image)    # 若能show, 则表示无误;若图片有问题这里会有error
    except:
        print(item+''+'have problem!')
        pass
    count = count + 1
    if count % 1000 == 0:    # 显示进度
        print(str(count) + '' + 'images has been detected.')
cv2.destroyAllWindows()

发布了47 篇原创文章 · 获赞 11 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qazwsxrx/article/details/103754773