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 - 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
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
python - Numpy array to PIL image format - Stack Overflow
I would suggest you follow the ... rescale (0-255), convert to uint8, etc. ... Save this answer. ... Show activity on this post. In my comments I meant that you do something like this: Copyimport numpy as np from PIL import Image img = np.array(image) mean = 0 # var = ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to convert numpy array into image using PIL? - Stack Overflow
import numpy as np from PIL import Image # Create black 1-bit array arr2 = np.full((200,200), False, dtype=bool) # Set some bits white arr2[80:120,80:120]=True im = Image.fromarray(arr2) im.save('a.png') print(im) More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ convert-a-numpy-array-to-an-image
Convert a NumPy array to an image - GeeksforGeeks
July 15, 2025 - The plt.imshow() function takes the NumPy array and renders it as an image. While not specifically focused on image processing, Matplotlibโ€™s visualization capabilities are convenient for displaying arrays as images, especially when analyzing ...
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
GitHub
github.com โ€บ mthiboust โ€บ array2image
GitHub - mthiboust/array2image: Converts a Numpy array to a PIL image. ยท GitHub
def array_to_image( arr, spatial_dims: tuple[int, ...] | None = None, channel_dim: int | None = None, cmap: Callable | None = None, inverted_colors: bool = False, bin_size: int | tuple[int, int] | None = None, target_total_size: int = 200, grid_thickness: int | tuple[int, ...] = 0, norm: bool = False, ) -> PIL.Image ... spatial_dims: Spatial dimensions of the array. If None, spatial dimensions are automatically guessed. channel_dim: Channel dimension of the array. Only 1, 2 or 3 channel dimension arrays can be converted to an image.
Find elsewhere
๐ŸŒ
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).
๐ŸŒ
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 โ€บ walsvid โ€บ e5048001b4ff89b6a7e1aec4b5bb41f1
How to convert between NumPy array and PIL Image ยท GitHub
How to convert between NumPy array and PIL Image. GitHub Gist: instantly share code, notes, and snippets.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ convert-a-numpy-array-to-an-image
Convert a NumPy array to an image
August 9, 2023 - In the following example, we are passing the array as the input argument to the Image() function of the PIL library then the array will be converted into an image. import numpy as np from PIL import Image img_array = np.random.randint(0, 256, ...
๐ŸŒ
YouTube
youtube.com โ€บ how to fix your computer
PYTHON : How to convert a NumPy array to PIL image applying matplotlib colormap - YouTube
PYTHON : How to convert a NumPy array to PIL image applying matplotlib colormap [ Gift : Animated Search Engine : https://www.hows.tech/p/recommended.html ] ...
Published: December 6, 2021
Views: 361
๐ŸŒ
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.

๐ŸŒ
YouTube
youtube.com โ€บ watch
How to convert a NumPy array to PIL image applying matplotlib colormap - YouTube
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn--Music by Eric Matyashttps://www.soundimage.orgTrack title: Cosmic P
Published: March 6, 2023
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ how to convert a numpy array to pil image applying matplotlib colormap
How to convert a NumPy array to PIL image applying matplotlib colormap | Codemia
September 24, 2025 - After conversion, PIL gives you simple save and resize operations, while NumPy remains ideal for numeric transforms. You can convert back with np.array(image) whenever downstream code expects an array.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 71142238 โ€บ how-to-convert-numpy-array-into-image-using-pil
How to convert numpy array into image using PIL? - Stack Overflow
import numpy as np from PIL import Image # Create black 1-bit array arr2 = np.full((200,200), False, dtype=bool) # Set some bits white arr2[80:120,80:120]=True im = Image.fromarray(arr2) im.save('a.png') print(im) <PIL.Image.Image image mode=1 ...
๐ŸŒ
Pluralsight
pluralsight.com โ€บ blog โ€บ tech guides & tutorials
Importing Image Data Into NumPy Arrays | Pluralsight
April 15, 2025 - For example, the code below loads the photograph in JPEG format and saves it in PNG format. import numpy as np from PIL import Image im = np.array(Image.open('kolala.jpeg').convert('L')) #you can pass multiple arguments in single line ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ [np and pil] how do you load images as array and then reshape back to view the image
r/learnpython on Reddit: [np and PIL] how do you load images as array and then reshape back to view the image
September 11, 2022 -
arr = np.zeros((20,5000))
for idx,file in enumerate(images):
    file_to_open = folder / file
    print(file_to_open)
    img = Image.open(file_to_open)
    img = img.reduce(4)
    print(img.size)
    arr[idx]=np.asarray(img).reshape((1,5000))

I have 19 images of 80 W x 61 H ( grayscale)I just want to stack 20x 5000 for machine learning.

examplebut problem occurs when I wanted to display only 1 image after the combine and I wasn't able to re-construct the image array to display the it as gif using the code below

Image.fromarray(arr[0].reshape((50,100),'L')

I think it didn't reshape properly..
Apologies if it's not entirely Python.

anyone familiar with PIL and reshape? Thanks