以下代码将此错误打印到终端但不会停止执行: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)
这是有问题的测试图像(是的,它是空白的):
有Google-Fu透露前段时间libpng的Anaconda包存在bug导致该问题,建议更新。我这样做没有效果,因此,为了更好的衡量,我也回去确保我的机器上安装了最新版本的 libpng。同样,没有变化。
除此之外,我已经尝试了多种不同的代码变体和操作条件,但仍然没有任何变化。具体来说,我试过:
- 将图像更改为
.jpg
。错误没有弹出,显然是因为它不再使用 libpng,但图像仍然作为 NoneType 返回。
- 传入额外的标志,如
cv2.IMREAD_GRAYSCALE
- 在 virtualenv 中安装 opencv(版本 3.4.4.19 和 3.4.5.20)和 numpy 并运行它。
- 使用 WSL 和 Raspberry Pi (3.5.3) 在我的 Windows 笔记本电脑 (Anaconda 3.6.5) 上运行它
顺便说一句,我断断续续地使用 OpenCV 已经两年多了,几乎没有问题,而且我无法让如此简单/愚蠢的东西工作,这让我的自尊心受到了伤害。我即将开始深入研究 libpng 文档,但如果您有任何意见或想法,我将不胜感激。
如果您在图像上运行 pngcheck
,它会告诉您 IEND
block 的校验和不正确:
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
我是一名优秀的程序员,十分优秀!