python读取图像会发生图像读取错误,一个说法是python自带读取图像的包版本不兼容导致。有时间还是要研究一下。没时间就简单粗暴点,直接把无法读取的图像清洗掉。
import cv2
import io
from PIL import Image
warnings.filterwarnings("error", category=UserWarning)
data_dir = "./data/train"
i = 0
def is_read_successfully(file):
try:
imgFile = Image.open(file)
return True
except Exception:
return False
for parent, dirs, files in os.walk(base_dir):
for file in files:
if not is_read_successfully(os.path.join(parent, file)):
print(os.path.join(parent, file))
os.remove(os.path.join(parent, file))
i = i + 1
print(i)