Quite a busy one-liner, but here it is:
- First ensure your NumPy array,
myarray, is normalised with the max value at1.0. - Apply the colormap directly to
myarray. - Rescale to the
0-255range. - Convert to integers, using
np.uint8(). - Use
Image.fromarray().
And you're done:
from PIL import Image
from matplotlib import cm
im = Image.fromarray(np.uint8(cm.gist_earth(myarray)*255))
with plt.savefig():

with im.save():

Quite a busy one-liner, but here it is:
- First ensure your NumPy array,
myarray, is normalised with the max value at1.0. - Apply the colormap directly to
myarray. - Rescale to the
0-255range. - Convert to integers, using
np.uint8(). - Use
Image.fromarray().
And you're done:
from PIL import Image
from matplotlib import cm
im = Image.fromarray(np.uint8(cm.gist_earth(myarray)*255))
with plt.savefig():

with im.save():

input = numpy_image
np.uint8 -> converts to integers
convert('RGB') -> converts to RGB
Image.fromarray -> returns an image object
from PIL import Image import numpy as np PIL_image = Image.fromarray(np.uint8(numpy_image)).convert('RGB') PIL_image = Image.fromarray(numpy_image.astype('uint8'), 'RGB')
python - How to read image from numpy array into PIL Image? - Stack Overflow
python - Converting a NumPy array to a PIL image - Stack Overflow
python - Numpy array to PIL image format - Stack Overflow
Converting between PIL Image and 2d pixel array?
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()
I need to do the following:
-
Import an image.
-
Convert image to a 2D array of pixel values (rgb tuples, bytes, whatever it doesn't matter).
-
Manipulate that 2D array (needs to be 2D as I'm using a library that requires a 2D array of data).
-
Convert the newly changed 2D array back into an image.
-
Save the image.
Anyone know how I can accomplish this? PIL Image documentation has some functions to get data but it's all 1D arrays, and I'm not sure how to get them into 2D arrays and then re-convert it back into an image again.
I'm trying to plot using matplotlib in Python, here is my code :
y = 3
f, axarr = plt.subplots(y,3)
f.set_figheight(150)
f.set_figwidth(150)
for i in range(0,y):
axarr[i,0].imshow(sats[i])
axarr[i,1].imshow(masks[i])
extra = np.array(Image.fromarray(sats[i], 'RGB'))
axarr[i,2].imshow(extra)
plt.show()What it gives me is this :
https://i.stack.imgur.com/nQxlD.png
The reason behind np.array(Image.fromarray(sats[i], 'RGB')) or numpy to PIL to numpy is I have to apply different function to that PIL image. And I may sometime implement some other methods for testing purpose.
But while reconstructing from numpy array the image becomes distorted.
What is actually wrong here, how this can be resolved ?