OpenCV imread 报告 libpng 错误: IEND: CRC 错误,无法加载图片。

4
以下代码会将错误信息输出到终端,但不会停止执行:libpng error: IEND: CRC error
import numpy as np
import cv2
import os

# image is stored in the same location as the python file
dir_path = os.path.dirname(os.path.realpath(__file__))
path = dir_path+'/test.png'

# img always gets set as a NoneType
img = cv2.imread(path)

这是有问题的测试图像(是的,它是空白的):enter image description here 一些Google搜索显示,Anaconda软件包中的libpng曾经存在一个错误,可能会导致此问题,并建议更新它。我已经这样做了,但没有效果,为了保险起见,我还回到我的计算机上确保最新版本的libpng已安装。仍然没有变化。
除此之外,我尝试了许多不同的代码变化和操作条件,但仍然没有变化。具体来说,我尝试过:
- 将图像更改为.jpg。错误不会弹出,显然是因为它不再使用libpng,但图像仍然返回为NoneType。 - 传递额外的标志,如cv2.IMREAD_GRAYSCALE - 在虚拟环境中安装opencv(版本3.4.4.19和3.4.5.20),并运行numpy。 - 在我的Windows笔记本电脑(Anaconda 3.6.5)上使用WSL和Raspberry Pi(3.5.3)运行它。
顺便说一句,我已经断断续续地使用OpenCV两年多了,几乎没有问题,现在我无法让这么简单/愚蠢的东西工作,这让我很难受。我即将开始深入研究libpng文档,但我非常感谢您提供的任何意见或想法。
1个回答

4
如果您在图像上运行pngcheck,它会告诉您IEND块的校验和不正确:
pngcheck -v blank.png 

输出

File: blank.png (79830 bytes)
  chunk IHDR at offset 0x0000c, length 13
    2560 x 1600 image, 32-bit RGB+alpha, non-interlaced
  chunk IDAT at offset 0x00025, length 8192
    zlib: deflated, 32K window, fast compression
  chunk IDAT at offset 0x02031, length 8192
  chunk IDAT at offset 0x0403d, length 8192
  chunk IDAT at offset 0x06049, length 8192
  chunk IDAT at offset 0x08055, length 8192
  chunk IDAT at offset 0x0a061, length 8192
  chunk IDAT at offset 0x0c06d, length 8192
  chunk IDAT at offset 0x0e079, length 8192
  chunk IDAT at offset 0x10085, length 8192
  chunk IDAT at offset 0x12091, length 5937
  chunk IEND at offset 0x137ce, length 0
  CRC error in chunk IEND (computed ae426082, expected ae426080)

如果你以十六进制格式转储文件:

xxd image.png > hex

然后在任何常规编辑器中编辑最后一个字节,使其正确,您可以使用以下命令重新构建文件:

xxd -r < hex > recovered.png

成功了!非常感谢! - Clayton Ramstedt
很酷 - 问题(和答案)都是免费的,所以如果你遇到困难,请回来问。祝你的项目好运! - Mark Setchell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接