检查损坏的图像(数据不完整)

以下代码能够检查出哪些图像导致 “image file is truncated” 和 “premature end of data segment” 错误。

 

images_dir = '/data/train_images'

# 返回 images_dir 目录下所有数据不完整图像的列表,支持多级目录
broken_images_dirs = []
for root, dirs, files in os.walk(images_dir):
    for filename in files:
        try:
            io.imread(os.path.join(root, filename))
        except Exception as e:
            print(e, filename)
            broken_images_dirs.append(filename)

 

你可能感兴趣的:(编程杂记)