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():

python - How to convert a NumPy array to PIL image applying matplotlib colormap - Stack Overflow
Converting between PIL Image and 2d pixel array?
Image collapse when converting a float tensor numpy image to PIL.Image.Image (all libraries)
Data Loss when converting images from np.array to PIL Image
Videos
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')
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.