Converting a NumPy array to a PIL image

Question:

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?

Asked By: Karnivaurus

||

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)
Answered By: 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()
Answered By: Ashish Kulkarni