libpng error

现象:

现象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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值