ERROR | Corrupt JPEG data: 111 extraneous bytes before marker 0xd9...

本文介绍了如何在使用OpenCV读取图片时遇到Corrupt JPEG数据错误,通过PIL库的Image.open()函数和类型转换解决,并提到了忽略特定警告的方法。重点在于解决实际问题并提供替代代码实例。
摘要由CSDN通过智能技术生成


问题描述
Corrupt JPEG data: 1 extraneous bytes before marker 0xdb
libpng warning: iCCP: known incorrect sRGB profile
Premature end of JPEG file
Premature end of JPEG file
Premature end of JPEG file
Premature end of JPEG file

使用opencv(python)读取图片时,报如上错误,据说是因为图片破损。try_except未能捕获该警告。该部分代码如下:

img = cv2.imread(img_path)
if img is None:
    img = np.zeros(shape=(224, 224, 3), dtype=np.uint8)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)


解决方案
使用PIL的读取函数。修改如下:

img = None
try:
    img = Image.open(img_path).convert("RGB") #1
except:
    pass
if img is None:
    img = np.zeros(shape=(224, 224, 3), dtype=np.uint8)
    img = Image.fromarray(img)

其中#1处添加.convert("RGB"),因为数据集含有RGBA(png)或者灰度图片,所有要进行类型转换。

如遇到如下警告,可使用warnings(warnings.filterwarnings(“ignore”))忽略。

PIL Palette images with Transparency expressed in bytes should be converted
————————————————
版权声明:本文为CSDN博主「lsh呵呵」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/nima1994/article/details/102560730

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值