欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

libpng error

程序员文章站 2022-07-14 17:10:36
...

现象:

现象1:可以正常获取类型
bool(imghdr.what(‘bad.png’)) #True

现象2: 不能读取,读取会报错,
img = cv2.imread(‘G:\tools\check\wrong_img\wrong_img\25.png’)
libpng error: bad adaptive filter value

img is None
True

现象3: 读取时不会立即报错,而是在程序执行完才报错
img = cv2.imread(“bad.png”)
img = cv2.imread(“30.png”)
print(“read end”)
if not img is None:
cv2.imshow(“img_roi”, img)
cv2.waitKey()

read end
libpng error: bad adaptive filter value

现象4: 换一种读入方式,会发现它只在解码时报错
r = open(file, ‘rb’).read() #可以读取
img_array = np.asarray(bytearray(r), dtype=np.uint8) # 可以转类型
img = cv2.imdecode(img_array, 1) # 解码出错 所以转换时要做解码判断

解决方案
使用 Image.open(path/to/image).tobytes() 触发错误并捕获它

I had some corrupted images, partially downloaded images to be precise, but i could not catch them using: imghdr.What(), Image.open().verify() or cv2.imread methods.

Only solution worked is this: Image.open(path/to/image).tobytes()

this code will throw error IOError if the image is corrupted.

hope it helps someone

        try:
            Image.open(path).tobytes()
        except IOError:
            print('detect error img %s' % path)
            continue