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
🌐
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.
🌐
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 - Run this code, and boom — you’ve got your image data ready in array form! ... Here comes the exciting part: transforming your NumPy array into an actual image! We’ll use Image.fromarray() from the PIL library.
Discussions

python - How to convert a NumPy array to PIL image applying matplotlib colormap - Stack Overflow
I want to take a NumPy 2D array which represents a grayscale image, and convert it to an RGB PIL image while applying some of the matplotlib colormaps. I can get a reasonable PNG output by using the More on stackoverflow.com
🌐 stackoverflow.com
python - How do I convert a PIL Image into a NumPy array? - Stack Overflow
How do I convert a PIL Image back and forth to a NumPy array so that I can do faster pixel-wise transformations than PIL's PixelAccess allows? I can convert it to a NumPy array via: pic = Image.open(& More on stackoverflow.com
🌐 stackoverflow.com
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
Converting between PIL Image and 2d pixel array?
functions to get data but it's all 1D arrays, You might mean the getdata() method which returns a sequence object which contains the image data. You can use that to create 2D data. You get the image row length from the Image.size attribute , read one row length of pixel data from the sequence and store it as one row in your 2D data. More on reddit.com
🌐 r/learnpython
5
3
May 11, 2022
🌐
GitHub
gist.github.com › 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.
🌐
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.
🌐
Delft Stack
delftstack.com › home › howto › matplotlib › convert a numpy array to pil image python
How to Convert a NumPy Array to PIL Image in Python | Delft Stack
February 2, 2024 - We use the Image.fromarray() function to convert the array back to the PIL image object and finally display the image object using the show() method. import numpy as np from PIL import Image array = np.random.randint(255, size=(400, 400), ...
Find elsewhere
🌐
GitHub
github.com › mthiboust › array2image
GitHub - mthiboust/array2image: Converts a Numpy array to a PIL image. · GitHub
November 22, 2023 - 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
Author   mthiboust
🌐
Roboflow
roboflow.com › use opencv › convert pil image to numpy array
Convert PIL Image to NumPy array (OpenCV)
HOW TO GUIDE · You can convert a PIL Image object into a NumPy array using the np.asarray() function: import numpy as np from PIL import Image pil_image = Image.open("image.jpeg") image = np.asarray(pil_image) OpenCV can be used with the open source supervision Python package.
🌐
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
import numpy def fig2data ( fig ): """ @brief Convert a Matplotlib figure to a 4D numpy array with RGBA channels and return it @param fig a matplotlib figure @return a numpy 3D array of RGBA values """ # draw the renderer fig.canvas.draw ( ) # Get the RGBA buffer from the figure w,h = fig.canvas.get_width_height() buf = numpy.fromstring ( fig.canvas.tostring_argb(), dtype=numpy.uint8 ) buf.shape = ( w, h,4 ) # canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode buf = numpy.roll ( buf, 3, axis = 2 ) return buf Conversion to a PIL image At this point, we just have to convert the numpy array to a PIL Image to end the conversion.
🌐
Educative
educative.io › answers › convert-a-pil-image-to-numpy-array
Convert a PIL image to NumPy array
Line 1: We import Image module from the PIL library. Line 3: We open the image using the Image.open() function and pass the file name of the image as a parameter. Line 5: We print the image, which shows that the image is a JPEG image.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-images-to-numpy-array
How to Convert images to NumPy array? - GeeksforGeeks
July 15, 2025 - To work with them in Python, we ... using a NumPy array is a table of numbers showing each pixel’s color. In this article, we’ll learn how to do this using popular Python tools. Let us check for an image that is in the PNG or JPEG format. The image can be referred via its path. Image class is the heart of PIL...
🌐
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).
🌐
Pillow Documentation
pillow.readthedocs.io › en › stable › releasenotes › 8.3.1.html
8.3.1 (2021-07-06) - Pillow (PIL Fork) 12.1.1 documentation
July 6, 2021 - This fixes a regression introduced in 8.3.0 when converting an image to a NumPy array with a dtype argument. >>> from PIL import Image >>> import numpy >>> im = Image.new("RGB", (100, 100)) >>> numpy.array(im, dtype=numpy.float64) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: __array__() takes 1 positional argument but 2 were given >>> In 8.3.0, a check to see if the destination was sys.stdout when saving an image was updated.
🌐
Pluralsight
pluralsight.com › what makes pluralsight different | pluralsight › 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 ...
🌐
TutorialsPoint
tutorialspoint.com › python_pillow › python_pillow_m_l_with_numpy.htm
Python Pillow - M L with Numpy
The mode is either explicitly specified or inferred from the dtype of the input array. In this example, a NumPy array is created, and then Image.fromarray() is used to create a Pillow Image from the NumPy array.
🌐
Python Examples
pythonexamples.org › python-pillow-convert-numpy-array-to-image
Python Pillow – Convert Numpy Array to Image
To convert a given NumPy array to an image using Pillow library in Python, call Image.fromarray() function and pass the given NumPy array as argument. The function returns a PIL Image object. You can then save this Image object as an image file in the disk storage.
🌐
Uploadcare
uploadcare.com › processing & transformations category › fast pillow image import to numpy and opencv arrays
Fast Pillow image import to NumPy and OpenCV arrays | Uploadcare
September 7, 2021 - If you look at the dependencies and code behind Pillow, you won’t find any mention of NumPy. (Well, you will, but only in the comments.) The same is true for NumPy. So how are images converted from one format to another? Turns out NumPy has a special interface for that.