Mohammad Moghimi
Mohammad Moghimi

Reputation: 4696

numpy.asarray( Image.open ( ...) ) does not work

I believe that numpy.asarray is the recommended way to create numpy arrays for images.

numpy.asarray(Image.open("cat.jpg"))

I have two python installations on my machine. One is locally installed on my home directory. and the one installed in /usr.

Anyways, the local installation does not work. The numpy creates an array of that JPEG object which is not want I need.

array(<JpegImagePlugin.JpegImageFile image mode=RGB size=1000x781 at 0x2395878>, dtype=object)

The other python installation output is a numpy array which is what I need.

array([[[ 89, 125,  51],
        [ 89, 125,  51],
        [ 90, 126,  52],
        ..., 
        [ 53,  55,  50],
        [ 53,  55,  50],
        [ 53,  55,  50]],

       [[ 89, 125,  51],
        [ 89, 125,  51],
        [ 90, 126,  52],
        ..., 
        [ 54,  56,  51],
        [ 53,  55,  50],
        [ 53,  55,  50]],

       ..., 


       [[132, 134, 147],
        [133, 135, 148],
        [133, 135, 148],
        ..., 
        [149, 165, 190],
        [149, 165, 190],
        [149, 165, 190]]], dtype=uint8)

Anyone knows a fix?

Upvotes: 1

Views: 2484

Answers (2)

maral
maral

Reputation: 1471

I had exactly the same issue: numpy.asarray didn't work on my local machine (that is, produced an array containing a single image object), while it worked on my server, although Python and PIL versions were the same in both places and Numpy version was newer on the local machine (the one on which numpy.asarray didn't work).

After performing some investigation, I finally discovered that this issue was because of PIL compiled without support for certain format: in my case, I was getting IOError: Decoder 'zip' not available when trying to print the __array_interface__ field of an image object returned by PIL on the local machine.

To install PIL with support for most common image formats, I just used the package from my system repository instead of installing PIL with pip and everything worked fine:

sudo pip uninstall PIL
sudo apt-get install python-pil

However, I also managed to make everything work fine using pip. I did as follows (taken from IOError: decoder zip not available).

First of all, I installed necessary libraries in my system:

sudo apt-get install libjpeg62-dev zlib1g-dev libfreetype6-dev

For some reason, pip didn't see these libraries, and they had to be linked to /usr/lib on my 64-bit machine:

sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib
sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib
sudo ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib

Since PIL installation still failed at that moment with an error concerning missing header file, I had to create another symlink (see Trying to install PIL "pip install PIL" and got this error.):

sudo ln -s /usr/include/freetype2 /usr/include/freetype

Then, I managed to reinstall PIL with pip:

pip install -U --force-reinstall PIL

The output from that command confirms that a support for JPEG and PNG formats is now included:

*** TKINTER support not available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- FREETYPE2 support available
*** LITTLECMS support not available

Upvotes: 0

YXD
YXD

Reputation: 32521

For reading and writing images with NumPy I use either

from scipy.misc import imread, imsave
image = imread("filename.jpg")

or

from skimage.io import imread, imsave

Rather than going through PIL/Pillow directly.

Upvotes: 2

Related Questions