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
🌐
Pillow Documentation
pillow.readthedocs.io › en › stable › reference › Image.html
Image module - Pillow (PIL Fork) 12.2.0 documentation
As with array support, when converting Pillow images to arrays, only pixel values are transferred.
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
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
Image collapse when converting a float tensor numpy image to PIL.Image.Image (all libraries)
System Info transformers version: 4.46.1 Platform: Windows-10-10.0.19045-SP0 Python version: 3.9.13 Huggingface_hub version: 0.26.2 Safetensors version: 0.4.3 Accelerate version: 1.0.1 Accelerate c... More on github.com
🌐 github.com
6
November 18, 2024
Data Loss when converting images from np.array to PIL Image
I want to use a CNN for classification of galaxies. For this, I intended to use the torchvision.transforms module to perform some data augmentation. However, for this purpose there are PIL Images required. So I have to convert my float type images to uint8 and hence I have lose some data. More on discuss.pytorch.org
🌐 discuss.pytorch.org
1
0
July 2, 2020
🌐
TutorialsPoint
tutorialspoint.com › article › convert-a-numpy-array-to-an-image
Convert a NumPy array to an image
August 9, 2023 - This library performs all the basic image processing tasks but don?t have any advanced features required for computer vision applications. We have a function in PIL named fromarray() which is used to convert the array into an image.
🌐
Saturn Cloud
saturncloud.io › blog › how-to-convert-rgb-pil-image-to-numpy-array-with-3-channels-a-comprehensive-guide
How to Convert RGB PIL Image to Numpy Array with 3 Channels: A Guide | Saturn Cloud Blog
February 8, 2024 - The resulting img_array is a 3D NumPy array, where the first dimension represents the height, the second dimension represents the width, and the third dimension represents the RGB channels. To verify that our conversion was successful, we can print the data types. ... Versatility: The combination of PIL and NumPy provides a versatile solution for handling and manipulating images, catering to a wide range of image processing tasks.
🌐
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.

Find elsewhere
🌐
GitHub
github.com › huggingface › transformers › issues › 34775
Image collapse when converting a float tensor numpy image to PIL.Image.Image (all libraries) · Issue #34775 · huggingface/transformers
November 18, 2024 - from transformers.image_transforms import pad import numpy as np import torch from PIL import Image # Example image as a NumPy array image = np.random.rand(224, 224, 3) # Height x Width x Channels image_pil = np.array(Image.fromarray(image, 'RGB')) # Open with PIL and save image_uint8 = (image * 255.0).astype(np.uint8) # Define padding: ((before_height, after_height), (before_width, after_width)) padding = ((0, 0), (112, 112)) # Pads width to make it 448 # Apply padding padded_image = pad(image, padding=padding) padded_image_pil = pad(image_pil, padding=padding) padded_image_uint8 = pad(image_
Author   John6666cat
🌐
PyTorch Forums
discuss.pytorch.org › t › data-loss-when-converting-images-from-np-array-to-pil-image › 87764
Data Loss when converting images from np.array to PIL Image - PyTorch Forums
July 2, 2020 - I want to use a CNN for classification of galaxies. For this, I intended to use the torchvision.transforms module to perform some data augmentation. However, for this purpose there are PIL Images required. So I have to convert my float type images to uint8 and hence I have lose some data.
🌐
Codecademy
codecademy.com › docs › python:pillow › image module › fromarray()
Python:Pillow | Image Module | fromarray() | Codecademy
June 8, 2024 - from PIL import Image · import numpy as np · # Defined an array 300 by 36 with 200 as all the pixel values · obj_array = np.full((300, 36), 200) # Create an image from the array, as our `obj_array' is stored in the form of a · # numbered array · new_imageA = Image.fromarray(obj_array, mode="CMYK") # Show the image generated using .fromarray() new_imageA.show() # Show the pixel value by the coordinate ·
🌐
GitHub
github.com › mthiboust › array2image
GitHub - mthiboust/array2image: Converts a Numpy array to a PIL image. · GitHub
November 22, 2023 - from array2image import array_to_image # Fix the bin size image = array_to_image( array[0, 0], bin_size=4 )
Author   mthiboust
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-a-numpy-array-to-an-image
Convert a NumPy array to an image - GeeksforGeeks
July 15, 2025 - This allows the array to be manipulated as a standard image, making it suitable for tasks like displaying, saving or further processing the image. ... import numpy as np from PIL import Image a = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8) # Convert `a` to a PIL Image img = Image.fromarray(a) img.show()
🌐
Blender Artists
blenderartists.org › coding › python support
Numpy array to image data - Python Support - Blender Artists Community
February 8, 2024 - so I have a 2d numpy array of float ... 8, 9)) img = bpy.data.images.new(name=“myimg”, width=array.shape[0], height=array.shape[1]) pixels = np.zeros(array.shape[0] * array.shape[1] * 4, dtype=“float32”) for i in range(len(array.shape[0])): for j in range(len(array.shape[1])): value = array[i, j] ...
🌐
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 - Here comes the exciting part: transforming your NumPy array into an actual image! We’ll use Image.fromarray() from the PIL library.
🌐
scikit-image
scikit-image.org › skimage-tutorials › lectures › 00_images_are_arrays.html
Images are numpy arrays — Image analysis in Python
E.g., we can make a red square by using standard array slicing and manipulation: cat[10:110, 10:110, :] = [255, 0, 0] # [red, green, blue] plt.imshow(cat); Images can also include transparent regions by adding a 4th dimension, called an alpha layer. from skimage import data img0 = data.chelsea() img1 = data.rocket()