使用scipy.ndimage.imread将图像文件加载到ndarray时出错



我正试图用这样的东西将图像文件加载到ndarray中:

image_data = ndimage.imread(image_file).astype(float)

但是我得到了这个错误:

/home/milos/anaconda3/envs/tensorflow/lib/python3.5/site-packages/scipy/ndimage/io.py in imread(fname, flatten, mode)
     23     if _have_pil:
     24         return _imread(fname, flatten, mode)
---> 25     raise ImportError("Could not import the Python Imaging Library (PIL)"
     26                       " required to load image files.  Please refer to"
     27                       " http://pypi.python.org/pypi/PIL/ for installation"
ImportError: Could not import the Python Imaging Library (PIL) required   to load image files.  Please refer to http://pypi.python.org/pypi/PIL/ for   installation instructions.

我在运行笔记本的环境中安装了Pillow,它也显示在pip冻结上。我也试着从控制台运行它,但也出现了类似的错误。

有什么办法解决这个问题吗?或者有没有其他方法可以将图像加载到ndarray中?

最终绕过scipy:

from PIL import Image
img = Image.open(image_file)
image_data = np.array(img).astype(float)

我仍然想知道scipy的问题是什么,所以如果你知道的话,请发布

编辑:

找到了更好的解决方案:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image_data = mpimg.imread(image_file)

这创建了一个numpy ndarray,并将像素深度标准化为0-1,如果我想做一个向后转换来检查它是否仍然良好,它会很好地工作:

plt.imshow(image_data)

我使用的是Python 2.7.x,遇到了同样的问题。我也不确定scipy.ndige.imread()在这种情况下是怎么回事。卸载Pillow(PIL)并不能解决问题。所以,我按照米洛斯的建议做了同样的事情,并且成功了:

import PIL
import numpy as np
image_file = './my_image.png'
image_data = np.array(PIL.Image.open(image_file)).astype(float)

最新更新