在自动驾驶行业中,图像的数据量一般很大,难免因为各种原因出现图像损坏的问题。
于是,如何在大量的图像中将损坏的图像找出来呢?
这里自己总结了两个方法,且这两个方法均是有效的。
PS:不适用于检测图像扩展名更改导致图像损坏打不开的情况。
import imghdr
import cv2
import os
from PIL import Image
path = '/home/songbing/Pictures'
for root,dirs,files in os.walk(path):
for file in files:
if file.endswith('.png') or file.endswith('.jpg'):
pic_path = os.path.join(root,file)
try:
'''方法一'''
#Image.open(pic_path).load()
'''方法二'''
img = Image.open(pic_path)
img.verify()
img.close()
except :
print('损坏: %s' % pic_path)
else:
print('正常:%s' % pic_path)