为什么scipy.ndimage.io.imread返回PngImageFile,而不是值数组

mwa*_*kom 8 python scipy

我有两台不同的机器,安装了scipy 0.12和PIL.在一台机器上,当我尝试读取.png文件时,它返回一个大小为整数的数组(wxhx 3):

In[2]:  from scipy.ndimage.io import imread
In[3]:  out = imread(png_file)
In[4]:  out.shape
Out[4]: (750, 1000, 4)
Run Code Online (Sandbox Code Playgroud)

在另一台机器上,使用相同的图像文件,这将返回一个PIL.PngImagePlugin.PngImageFile包装在数组中的对象

In[2]: from scipy.ndimage.io import imread
In[3]: out = imread(png_file)
In[4]: out.shape
Out[4]: ()
In[5]:  out
Out[5]: array(<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1000x750 at 0x1D40050>, dtype=object)
Run Code Online (Sandbox Code Playgroud)

我看不到任何方法来访问后一个对象的数据.

我有一种模糊的感觉,即PIL使用Png库来读取图像的方式有问题,但是有哪些更具体的错误会导致这种行为?

Ken*_*eld 8

您可能有一个不完整的Python成像库(PIL)安装,SciPy依赖它来读取图像.PIL依赖于libjpeg加载JPEG图像和zlib加载PNG图像,但可以在没有任何图像的情况下安装(在这种情况下,它无法加载库中缺少的任何图像).

我有与上面描述的JPEG图像完全相同的问题.不会引发任何错误消息,而是SciPy调用只返回一个包装的PIL对象,而不是正确地将图像加载到数组中,这使调试变得特别棘手.但是,当我尝试直接使用PIL加载图像时,我得到了:

> import Image
> im = Image.open('001988.jpg')
> im
   <JpegImagePlugin.JpegImageFile image mode=RGB size=333x500 at 0x20C8CB0>
> im.size
> (333, 500)
> pixels = im.load()
   IOError: decoder jpeg not available
Run Code Online (Sandbox Code Playgroud)

所以我卸载了我的PIL副本,安装了丢失的libjpeg(在我的情况下,可能zlib是你的),重新安装PIL以注册库的存在,现在加载SciPy的图像完美地工作:

> from scipy import ndimage
> im = ndimage.imread('001988.jpg')
> im.shape
   (500, 333, 3)
> im
   array([[[112, 89, 48], ...
                     ..., dtype=uint8)
Run Code Online (Sandbox Code Playgroud)


dan*_*van 5

当您拥有旧版本的python映像库或更糟糕的安装时,通常会发生此错误(imread返回PIL.PngImagePlugin.PngImageFile类而不是数据数组).是一个更新的"友好"的分叉,绝对值得安装!pillowPILpillowPIL

尝试更新这些包; (取决于你的python发行版)

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

然后尝试重新启动python shell并再次运行命令.