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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/nima1994/article/details/102560730

问题描述

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

猜你喜欢

转载自blog.csdn.net/nima1994/article/details/102560730
111