python+检查图像文件是否损坏,是,删除

 

import os
import cv2
import numpy as np

path = "./test_img/"
imglist = os.listdir(path)
for fi in imglist:
    img_folder = path+fi
    print(img_folder)
    image = cv2.imdecode(np.fromfile(img_folder, dtype=np.uint8), -1)
    try:
        image.shape 
    except:
        print('fail to read',img_folder)
        os.remove(img_folder)
        continue

 

posted @ 2021-07-18 17:01  皮卡皮卡妞  阅读(360)  评论(0编辑  收藏  举报