Milos Matovic Milos Matovic - 1 month ago 50
Python Question

Error loading a image file into ndarray with scipy.ndimage.imread

Im trying to load a image file into a ndarray with something like this:

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

but i get this error:

/home/milos/anaconda3/envs/tensorflow/lib/python3.5/site-packages/scipy/ndimage/ 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 " for installation"

ImportError: Could not import the Python Imaging Library (PIL) required to load image files. Please refer to for installation instructions.

I have Pillow installed inside the environment from which im running the notebook, it also shows up on pip freeze.
I also tried running it from the console but got similar error.

Any ideas how to fix this? Or is there a alternative way to load a image into ndarray?


Managed to do it in the end by bypassing scipy :

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

would still like to know what the problem is with scipy, so please post if you know it

Edit :

Found a better solution:

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

this creates a numpy ndarray and normalizes the pixel depths to 0-1, and it worked nicely if i wanted to do a backwards conversion to check if its still good: