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?
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.
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.
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.
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.
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 ().
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.
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)
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With