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

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

Answer from fraxel on Stack Overflow
Discussions

python - How to read image from numpy array into PIL Image? - Stack Overflow
I am trying to read an image from a numpy array using PIL, by doing the following: from PIL import Image import numpy as np #img is a np array with shape (3,256,256) Image.fromarray(img) and am ge... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Converting a NumPy array to a PIL image - Stack Overflow
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- More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Numpy array to PIL image format - Stack Overflow
I'm trying to convert an image from a numpy array format to a PIL one. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Converting between PIL Image and 2d pixel array?
Are you trying to translate a image into ascii art? More on reddit.com
๐ŸŒ r/learnpython
5
3
May 11, 2022
๐ŸŒ
Delft Stack
delftstack.com โ€บ "delft stack" โ€บ "howto" โ€บ "python matplotlib howto's" โ€บ "how to convert a numpy array to pil image in python"
How to Convert a NumPy Array to PIL Image in Python | Delft Stack
March 13, 2025 - This tutorial explains how to convert a NumPy array to a PIL image using the Image.fromarray() function from the PIL package. Learn step-by-step methods for handling both RGB and grayscale images, and discover how to customize image properties ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ convert-a-numpy-array-to-an-image
Convert a NumPy array to an image - GeeksforGeeks
July 15, 2025 - The Image.fromarray() function is used for this task, which takes a NumPy array as input and creates a PIL Image object.
๐ŸŒ
Pythoninformer
pythoninformer.com โ€บ python-libraries โ€บ numpy โ€บ numpy-and-images
PythonInformer - Image processing with pillow and NumPy
October 2, 2022 - Now let's fill the array with orange pixels (red=255, green=128, blue=0). We use slices to do this, the three values are broadcast across all the rows and columns of the array: ... The PIL function Image.fromarray function creates a PIL image from a NumPy array.
๐ŸŒ
GitHub
gist.github.com โ€บ e5048001b4ff89b6a7e1aec4b5bb41f1
How to convert between NumPy array and PIL Image ยท GitHub
How to convert between NumPy array and PIL Image ยท Raw ยท NumPy2PIL.py ยท This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
๐ŸŒ
Medium
medium.com โ€บ @whyamit101 โ€บ step-by-step-guide-to-convert-numpy-array-to-pil-image-f7f8492cd785
Step-by-Step Guide to Convert NumPy Array to PIL Image | by why amit | Medium
February 9, 2025 - Grayscale images are simpler to handle, with just one channel. You created a grayscale NumPy array and converted it to a PIL image using the 'L' mode.
Find elsewhere
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ converting-numpy-arrays-to-images-using-cv2-and-pil
Converting Numpy Arrays to Images using CV2 and PIL | Saturn Cloud Blog
May 1, 2026 - In this code, we first create a 100x200 Numpy array with three channels (for RGB). We then fill the left half of the array with orange and the right half with blue. Finally, we use cv2.imwrite() to save the array as an image. The Python Imaging Library (PIL) is another powerful library for opening, manipulating, and saving many different image file formats.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ convert-a-numpy-array-to-an-image
Convert a NumPy array to an image
August 9, 2023 - Following is the syntax for using the PIL library fromarray() function to convert the array into an image.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ python-numpy-exercise-109.php
Python NumPy: Convert a numpy array to an image - w3resource
August 29, 2025 - img = Image.fromarray(data, 'RGB'): Convert the NumPy array 'data' to a PIL Image object with the mode 'RGB' (red, green, blue).
๐ŸŒ
Codegive
codegive.com โ€บ blog โ€บ numpy_array_to_image_pil.php
Numpy array to image pil
Computer Vision: Implement custom algorithms using NumPy, then integrate them into broader image processing pipelines with PIL. Data Visualization: Generate images programmatically from numerical data. This tutorial will provide a deep dive into this essential conversion, covering everything from basic grayscale images to complex RGBA images with transparency. Before you begin, make sure you have the following libraries installed: NumPy: For numerical operations and array manipulation.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ converting between pil image and 2d pixel array?
r/learnpython on Reddit: Converting between PIL Image and 2d pixel array?
May 11, 2022 -

I need to do the following:

  1. Import an image.

  2. Convert image to a 2D array of pixel values (rgb tuples, bytes, whatever it doesn't matter).

  3. Manipulate that 2D array (needs to be 2D as I'm using a library that requires a 2D array of data).

  4. Convert the newly changed 2D array back into an image.

  5. 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.

๐ŸŒ
Univ-lille
icare.univ-lille.fr โ€บ how-to-convert-a-matplotlib-figure-to-a-numpy-array-or-a-pil-image
How to convert a matplotlib figure to a numpy array or a PIL image โ€“ ICARE Data and Services Center
This is managed by the function ... @return a Python Imaging Library ( PIL ) image """ # put the figure pixmap into a numpy array buf = fig2data ( fig ) w, h, d = buf.shape return Image.fromstring( "RGBA", ( w ,h ), buf.tostring( ) ) This will return a PIL Image that you ...
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ 5 best ways to convert python numpy array to image
5 Best Ways to Convert Python NumPy Array to Image - Be on the Right Side of Change
February 20, 2024 - The SciPy library provides a straightforward method to save NumPy arrays as images using the scipy.misc.imsave() function, which is however deprecated in recent versions of SciPy in favor of imageio or Pillow.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-convert-images-to-numpy-array
How to Convert images to NumPy array? - GeeksforGeeks
July 15, 2025 - Explanation: It converts a PIL image to a NumPy array using img_to_array() for processing, then back to a PIL image using array_to_img(), confirming smooth transformation with array type float32 and shape (200, 400, 3).
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ numpy to pil to numpy, image become distorted
r/learnpython on Reddit: numpy to pil to numpy, image become distorted
October 3, 2020 -

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 ?