首页 > 解决方案 > scikit-image 无法导入图像

问题描述

环境:

源代码:

from skimage.io import imread
file_path = "image_you_like.png"
img = imread(file_path, plugin="pil", as_gray=False)

预期结果:

img是图像的 numpy 数组。

实际结果:

不要相信行号,因为我插入了几个打印函数。

~/Python/experiment/venv/lib/python3.7/site-packages/PIL/PngImagePlugin.py in _seek(self, frame, rewind)
    789 
    790             try:
--> 791                 cid, pos, length = self.png.read()
    792             except (struct.error, SyntaxError):
    793                 break

AttributeError: 'NoneType' object has no attribute 'read'

问题

如何防止此错误并读取图像?

经过一些分析,我发现imread如果plugin="matplotlib"plugin="imageio". 问题是某些功能,例如skimage.data.camera调用imreadplugin="pil"因此我无法尝试任何示例图像scikit-image

[编辑]

from skimage.io.manage_plugins import plugin_store
print(plugin_store["imread"])

打印以下内容

[('imageio', <function imread at 0x7efe39112c20>), ('matplotlib', <function imread at 0x7efe37f51290>)]

所以在列表中找不到“pil”。

标签: pythonpython-imaging-libraryscikit-image

解决方案


此问题已在 github 上报告

作为用户,最简单的解决方案是将版本降级到 6.2.2。


推荐阅读