Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

scipy

I have two different machines with scipy 0.12 and PIL installed. On one machine, when I try to read a .png file, it returns an array of integers with size (w x h x 3):

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

On the other machine, using the same image file, this returns a PIL.PngImagePlugin.PngImageFile object wrapped in an array

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)

I can't see any way to access the data for the latter object.

I have the vague sense that something is wrong with the way PIL is using Png libraries to read the image, but is there something more specific that would be wrong and cause this behavior?

like image 951
mwaskom Avatar asked Sep 26 '13 18:09

mwaskom


2 Answers

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)
like image 122
Ken Chatfield Avatar answered Oct 28 '22 16:10

Ken Chatfield


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.

like image 5
danodonovan Avatar answered Oct 28 '22 17:10

danodonovan