Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a NumPy array to a PIL image

I want to create a PIL image from a NumPy array. Here is my attempt:

# Create a NumPy array, which has four elements. The top-left should be pure 
# red, the top-right should be pure blue, the bottom-left should be pure green, 
# and the bottom-right should be yellow.
pixels = np.array([[[255, 0, 0], [0, 255, 0]], [[0, 0, 255], [255, 255, 0]]])

# Create a PIL image from the NumPy array
image = Image.fromarray(pixels, 'RGB')

# Print out the pixel values
print image.getpixel((0, 0))
print image.getpixel((0, 1))
print image.getpixel((1, 0))
print image.getpixel((1, 1))

# Save the image
image.save('image.png')

However, the print out gives the following:

(255, 0, 0)
(0, 0, 0)
(0, 0, 0)
(0, 0, 0)

And the saved image has pure red in the top-left, but all the other pixels are black. Why are these other pixels not retaining the colour I have assigned to them in the NumPy array?

like image 784
Karnivaurus Avatar asked Feb 07 '17 17:02

Karnivaurus


People also ask

Does pillow use NumPy?

In this chapter, we use numpy to store and manipulate image data using python imaging library – “pillow”. Note − This works only if you have PIP installed and updated.

What is PIL image Fromarray?

fromarray() from the PIL package. The Python Imaging Library ( PIL ) is a library in Python with various image processing functions. The Image. fromarray() function takes the array object as the input and returns the image object made from the array object.

How to convert a NumPy array to image in Python?

Here, we are going to use the Python Imaging Library ( PIL ) Module and Numerical Python (Numpy) Module to convert a Numpy Array to Image in Python. PIL and Numpy consist of various Classes. We require only Image Class. Hence, our first script will be as follows: Here, we have imported Image Class from PIL Module and Numpy Module as np.

How to convert array to PiL image in Java?

We use the Image.fromarray () function to convert the array back to the PIL image object and finally display the image object using the show () method.

How to convert a colormap to an integer in NumPy?

Quite a busy one-liner, but here it is: 1 First ensure your NumPy array, myarray, is normalised with the max value at 1.0. 2 Apply the colormap directly to myarray. 3 Rescale to the 0-255 range. 4 Convert to integers, using np.uint8 (). 5 Use Image.fromarray ().

What is the Python Imaging Library (PIL)?

The Python Imaging Library ( PIL) is a library in Python with various image processing functions. The Image.fromarray () function takes the array object as the input and returns the image object made from the array object.


2 Answers

The RGB mode is expecting 8-bit values, so just casting your array should fix the problem:

In [25]: image = Image.fromarray(pixels.astype('uint8'), 'RGB')
    ...:
    ...: # Print out the pixel values
    ...: print image.getpixel((0, 0))
    ...: print image.getpixel((0, 1))
    ...: print image.getpixel((1, 0))
    ...: print image.getpixel((1, 1))
    ...:
(255, 0, 0)
(0, 0, 255)
(0, 255, 0)
(255, 255, 0)
like image 199
Randy Avatar answered Oct 11 '22 23:10

Randy


Your numpy array should be of the form:

[[[248 248 248] # R G B
[248 248 248]
[249 249 249]
...
[ 79  76  45]
[ 79  76  45]
[ 78  75  44]]

[[247 247 247]
[247 247 247]
[248 248 248]
...
[ 80  77  46]
[ 79  76  45]
[ 79  76  45]]  

...
         
[[148 121  92]
[149 122  93]
[153 126  97]
...
[126 117 100]
[126 117 100]
[125 116  99]]]

Assuming you have your numpy array stored in np_arr, here is how to convert it to a pillow Image:

from PIL import Image
import numpy as np
new_im = Image.fromarray(np_arr)

To show the new image, use:

new_im.show()
like image 1
Ashish Kulkarni Avatar answered Oct 11 '22 22:10

Ashish Kulkarni