Corrupt JPEG data: 111 extraneous bytes before marker 0xd9...
  yKHDG1F9AQlS 2023年11月02日 114 0


问题描述

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


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
yKHDG1F9AQlS