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')
You're not saying how exactly putdata() is not behaving. I'm assuming you're doing
>>> pic.putdata(a)
Traceback (most recent call last):
File "...blablabla.../PIL/Image.py", line 1185, in putdata
self.im.putdata(data, scale, offset)
SystemError: new style getargs format but argument is not a tuple
This is because putdata expects a sequence of tuples and you're giving it a numpy array. This
>>> data = list(tuple(pixel) for pixel in pix)
>>> pic.putdata(data)
will work but it is very slow.
As of PIL 1.1.6, the "proper" way to convert between images and numpy arrays is simply
>>> pix = numpy.array(pic)
although the resulting array is in a different format than yours (3-d array or rows/columns/rgb in this case).
Then, after you make your changes to the array, you should be able to do either pic.putdata(pix) or create a new image with Image.fromarray(pix).
Open I as an array:
>>> I = numpy.asarray(PIL.Image.open('test.jpg'))
Do some stuff to I, then, convert it back to an image:
>>> im = PIL.Image.fromarray(numpy.uint8(I))
Source: Filter numpy images with FFT, Python
If you want to do it explicitly for some reason, there are pil2array() and array2pil() functions using getdata() on this page in correlation.zip.
python - Converting a NumPy array to a PIL image - Stack Overflow
Converting between PIL Image and 2d pixel array?
python - Numpy array to PIL image format - Stack Overflow
How to convert numpy array into image using PIL? - Stack Overflow
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.