With PIL it should work like this

from PIL import Image

I8 = (((I - I.min()) / (I.max() - I.min())) * 255.9).astype(np.uint8)

img = Image.fromarray(I8)
img.save("file.png")
Answer from eickenberg on Stack Overflow
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to convert a python numpy array to a grayscale image
5 Best Ways to Convert a Python NumPy Array to a Grayscale Image - Be on the Right Side of Change
February 20, 2024 - The imshow() function displays the image based on the array values and the savefig() function saves the displayed image to a file. This method is simple and effective for quick visualization and saving.
Discussions

python - How to save 2D float numpy arrays losslessly into a grayscale image while preserving resolution? - Stack Overflow
I have some Temperature field values ... array, then the conversion should be a grayscale (100x100) image).Then I will process the image and convert it back to Temperature numpy array. The image processing part can be considered as a black-box process.... More on stackoverflow.com
🌐 stackoverflow.com
python - Saving a Numpy array as an image - Stack Overflow
Save this answer. ... Show activity on this post. Imageio is a Python library that provides an easy interface to read and write a wide range of image data, including animated images, video, volumetric data, and scientific formats. It is cross-platform, runs on Python 2.7 and 3.4+, and is easy to install. ... Copyimport numpy as np import imageio # data is numpy array with grayscale ... More on stackoverflow.com
🌐 stackoverflow.com
December 17, 2011
python - Converting 2D Numpy array of grayscale values to a PIL image - Stack Overflow
Say I have a 2D Numpy array of values on the range 0 to 1, which represents a grayscale image. How do I then convert this into a PIL Image object? All attempts so far have yielded extremely strange More on stackoverflow.com
🌐 stackoverflow.com
python - Saving a Numpy array as an image (instructions) - Stack Overflow
I found my answer in a previous post: Saving a Numpy array as an image. The only problem being, there isn't much instruction on using the PyPNG module. There are only a few examples online-- http:// More on stackoverflow.com
🌐 stackoverflow.com
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-save-an-array-as-a-grayscale-image-with-matplotlib-numpy
How to save an array as a grayscale image with Matplotlib/Numpy?
March 25, 2026 - Here's how to create and save a grayscale image from a NumPy array ? import numpy as np import matplotlib.pyplot as plt # Set figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create random data with 5x5 dimension arr = np.random.rand(5, 5) # Display as grayscale image plt.imshow(arr, cmap='gray') plt.colorbar() # Add colorbar to show intensity scale plt.title('Grayscale Image from Array') # Save the image plt.savefig('/images/grayscale_output.png', dpi=300, bbox_inches='tight') plt.show()
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python numpy howtos" › "how to save numpy array as image in python"
How to Save NumPy Array as Image in Python | Delft Stack
February 2, 2024 - Here, image_data is a 3x3 NumPy array representing a grayscale image with pixel intensity values ranging from 0 to 255. Then, use the imageio.imwrite() function to save the NumPy array as an image.
🌐
Quora
quora.com › How-do-I-turn-NumPY-arrays-into-grayscale-images
How to turn NumPY arrays into grayscale images - Quora
Answer (1 of 3): There are several available libraries and examples of using them. They are easily found. Seek and ye shall find. [code]from PIL import Image import matplotlib.pyplot as plt import imageio [/code]You don’t use all three, but you can choose among them.
🌐
TutorialsPoint
tutorialspoint.com › how-to-show-numpy-2d-array-as-grayscale-image-in-jupyter-notebook
How to show numpy 2D array as grayscale image in Jupyter Notebook?
To show a 2D array as a grayscale image in Jupyter Notebook, we can take the following steps · Set the figure size and adjust the padding between and around the subplots. Create a random data using numpy. Display the data as an image, i.e., on a 2D regular raster, with gray colormap.
🌐
YouTube
youtube.com › mixible
how to save an array as a grayscale image with matplotlibnumpy - YouTube
Welcome to Mixible, your go-to source for comprehensive and informative content covering a broad range of topics from Stack Exchange websites. Our channel is...
Published: August 28, 2023
Views: 2
🌐
CSDN
devpress.csdn.net › python › 63045b9fc67703293080bc55.html
how to save an array as a grayscale image with matplotlib/numpy?_python_Mangs-Python
I simply thought that the pyplot.imsave function would do the job but it's not, it somehow converts my array into an RGB image. I tried to force the colormap to Gray during conversion but eventhough the saved image appears in grayscale, it still has a 128x128x4 dimension. Here is a code sample I wrote to show the behaviour : import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mplimg from matplotlib import cm x_tot = 10e-3 nx = 128 x = np.arange(-x_tot/2, x_tot/2, x_tot/nx) [X, Y] = np.meshgrid(x,x) R = np.sqrt(X**2 + Y**2) diam = 5e-3 I = np.exp(-2*(2*R/diam)**4) plt.figure() plt.imshow(I, extent = [-x_tot/2, x_tot/2, -x_tot/2, x_tot/2]) print I.shape plt.imsave('image.png', I) I2 = plt.imread('image.png') print I2.shape mplimg.imsave('image2.png',np.uint8(I), cmap = cm.gray) testImg = plt.imread('image2.png') print testImg.shape
Find elsewhere
🌐
scikit-image
scikit-image.org › skimage-tutorials › lectures › 00_images_are_arrays.html
Images are numpy arrays — Image analysis in Python
These are just NumPy arrays. 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() import matplotlib.pyplot as plt f, (ax0, ax1) = plt.subplots(1, 2, figsize=(20, 10)) ax0.imshow(img0) ax0.set_title('Cat', fontsize=18) ax0.axis('off') ax1.imshow(img1) ax1.set_title('Rocket', fontsize=18) ax1.set_xlabel(r'Launching position $\alpha=320$') ax1.vlines([202, 300], 0, img1.shape[0], colors='magenta', linewidth=3, label='Side tower position') ax1.plot([168, 190, 200], [400, 200, 300], color='white', linestyle='--', label='Side angle') ax1.legend();
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
Image processing with Python, NumPy | note.nkmk.me
October 20, 2020 - NumPy: astype() to change dtype of an array · Passing ndarray to Image.fromarray() returns PIL.Image. It can be saved as an image file with save() method. The format of the saved file is automatically determined from the extension of the path ...
🌐
Google Groups
groups.google.com › g › scikit-image › c › y7GdVM7xd7Q
Converting a numpy array to grayscale
Here is a snippet of code: >>> gray = ndimage.imread('1.jpg',flatten=True) >>> gray /= gray.max() >>> gray array([[ 0.30133331, 0.2895686 , 0.28172547, ... ... >>> gray2 = color.rgb2gray(rgb) >>> gray2 array([[ 0.31065608, 0.29889137, 0.29104824, ..., ... I believe this difference is causing the problem. Note if I convert the image to grayscale using a external tool and read this in then the values of the numpy array match similar/same to Matlab matrix. So what is the difference between converting an array to gray scale verse reading it in as grayscale?
🌐
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 - This might surprise you, but most issues stem from two simple mistakes: Shape mismatch errors If your array doesn’t have the correct shape, PIL won’t know how to interpret it. For example: A 2D array works for grayscale images (mode='L').
🌐
w3tutorials
w3tutorials.net › blog › saving-a-numpy-array-as-an-image-instructions
How to Save a Numpy Array as an Image with PyPNG: Step-by-Step Instructions to Fix .write() TypeError — w3tutorials.net
Import PyPNG and open a file in binary write mode ('wb'). ... Write the pixel data using writer.write(file, pixel_array). import png # Grayscale array from Step 2: shape (200, 200), dtype uint8 with open("grayscale_gradient.png", "wb") as f: ...
🌐
Codemia
codemia.io › home › knowledge hub › saving a numpy array as an image
Saving a Numpy array as an image | Codemia
September 23, 2025 - 1from PIL import Image 2import ... 8img = Image.fromarray(array) 9img.save("output.png") For grayscale images, pass a 2D array or specify the mode: python ·...