Skimage简单imread图像返回奇怪输出

2 投票
1 回答
1051 浏览
提问于 2025-04-18 08:54

我正在使用skimage模块来读取图像,但对我得到的输出有些困惑。
我尝试以以下方式读取一个简单的RGB图像“lena.png”:

from skimage import io
im = io.imread('lena.png')

我得到的输出具有以下特性:

In [49]: im
Out[49]: array(<PIL.PngImagePlugin.PngImageFile image mode=RGB size=512x512 at 0
x35FBD00>, dtype=object)

In [50]: im.shape
Out[50]: ()

In [51]: im.size
Out[51]: 1

现在我不明白,为什么im的形状不是(512,512,3)?
这段代码是我使用的一个更大程序的一部分,当某个函数尝试通过访问im.shape[2]来确定颜色通道的数量时,我遇到了错误。

我附上一个参考链接,里面有预期的输出:
请查看 http://scipy-lectures.github.io/packages/scikit-image/ 第3.2.2.2段落

>>> lena = data.lena()
>>> lena.shape
(512, 512, 3)

1 个回答

0

这个错误(imread 返回的是 PIL.PngImagePlugin.PngImageFile 类,而不是数据数组)通常发生在你安装了旧版本的 Python 图像库 pillow,或者更糟的是,安装了 PILpillowPIL 的一个更新版,使用起来更友好,绝对值得安装!

试着更新这些软件包;(具体步骤可能根据你的 Python 版本不同而有所不同)

# to uninstall PIL (if it's there, harmless if not)
$ pip uninstall PIL
# to install (or -U update) pillow
$ pip install -U pillow

然后再尝试重启你的 Python 环境,并重新运行这些命令。

撰写回答