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

Mil*_*vic 4 python scipy

我试图将图像文件加载到 ndarray 中,如下所示:

image_data = ndimage.imread(image_file).astype(float)
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:

/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.
Run Code Online (Sandbox Code Playgroud)

我在运行笔记本的环境中安装了 Pillow,它也显示在 pip freeze 中。我也尝试从控制台运行它,但遇到了类似的错误。

任何想法如何解决这一问题?或者有没有其他方法可以将图像加载到 ndarray 中?

Mil*_*vic 5

最后通过绕过 scipy 设法做到了:

from PIL import Image
img = Image.open(image_file)
image_data = np.array(img).astype(float)
Run Code Online (Sandbox Code Playgroud)

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

编辑 :

找到了更好的解决方案:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image_data = mpimg.imread(image_file)
Run Code Online (Sandbox Code Playgroud)

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

plt.imshow(image_data)
Run Code Online (Sandbox Code Playgroud)