Corrupt JPEG data: 2 extraneous bytes before marker 0x

参考:http://www.voidcn.com/article/p-dusgpwtd-bpv.html

cv2.imread(absolute_path) 有时会报错:

Corrupt JPEG data: 2 extraneous bytes before marker 0xd9

‘Premature end of JPEG file’是使用cv2(python版的opencv)读取图片时出现的警告,警告代码位于libjpeg中,

JMESSAGE(JWRN_JFIF_MAJOR, "Warning: unknown JFIF revision number %d.%02d")
184	JMESSAGE(JWRN_JPEG_EOF, "Premature end of JPEG file")
185	JMESSAGE(JWRN_MUST_RESYNC,
186	         "Corrupt JPEG data: found marker 0x%02x instead of RST%d")

具体链接为https://github.com/opencv/opencv/search?utf8=%E2%9C%93&q=JWRN_JPEG_EOF

在调试定位图像时,确实发现图像有破损。

二、解决方法

为了让python代码捕捉异常,程序修改如下:

        try:
            img = Image.open(absolute_path)
        except IOError:
            print(absolute_path)
        try:
            img= np.asarray(img)
        except :
            print('corrupt img',absolute_path)
absolute_path为文件的完整路径(绝对路径),即文件路径+文件名

你可能感兴趣的:(python,opencv)