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