[Python]Premature end of JPEG file-受损jpg文件检测

最近在网上下了一批jpg图片作为训练集,发现使用opencv的imread读取图片时总是出现Premature end of JPEG file报错,查找了网上资料发现时jpg图像受损导致,遂问gpt写了一段代码用以检测错误图片:

import os
import cv2
from skimage.io import imread
def check_image_integrity(image_path):
    try:
        img = imread(image_path)
        return False
    except Exception as e:
        print(f"Error reading file {image_path}: {e}")
        return True

def check_images_in_folder(folder_path):
    intact_images = []
    corrupted_images = []
    for filename in os.listdir(folder_path):
        if filename.endswith(('.jpg', '.jpeg', '.png', '.bmp', '.gif')):
            image_path = os.path.join(folder_path, filename)
            check_image_integrity(image_path)

    return intact_images, corrupted_images

# 示例用法
folder_path = "your_folder"
intact_images, corrupted_images = check_images_in_folder(folder_path)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值