Why does scipy.ndimage.io.imread return PngImageFile, not an array of values

It's likely that you have an incomplete Python Imaging Library (PIL) install, which SciPy relies on to read the image. PIL relies on the libjpeg package to load JPEG images and the zlib package to load PNG images, but can be installed without either (in which case it is unable to load whatever images the libraries are missing for).

I had exactly the same issue as you describe above for JPEG images. No error messages are raised, but rather the SciPy call just returns a wrapped PIL object rather than loading the image into an array properly, which makes this particularly tricky to debug. However, when I tried loading in the image using PIL directly, I got:

> 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

So I uninstalled my copy of PIL, installed the missing libjpeg (in my case, probably zlib in yours), reinstalled PIL to register the presence of the library, and now loading images with SciPy works perfectly:

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

This error (imread returning an PIL.PngImagePlugin.PngImageFile class rather than a data array) often happens when you have older versions of the python imaging library pillow or worse still PIL installed. pillow is an updated "friendly" fork of PIL and definitely worth installing!

Try updating these packages; (depending on your python distribution)

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

and then try restarting your python shell and running the commands again.

Tags:

Python

Scipy