I think the answer is wrong. The Image.fromarray( ____ , 'L') function seems to only work properly with an array of integers between 0 and 255. I use the np.uint8 function for this.

You can see this demonstrated if you try to make a gradient.

import numpy as np
from PIL import Image

# gradient between 0 and 1 for 256*256
array = np.linspace(0,1,256*256)

# reshape to 2d
mat = np.reshape(array,(256,256))

# Creates PIL image
img = Image.fromarray(np.uint8(mat * 255) , 'L')
img.show()

Makes a clean gradient

vs

import numpy as np
from PIL import Image

# gradient between 0 and 1 for 256*256
array = np.linspace(0,1,256*256)

# reshape to 2d
mat = np.reshape(array,(256,256))

# Creates PIL image
img = Image.fromarray( mat , 'L')
img.show()

Has the same kind of artifacting.

Answer from Nathan 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 desired output is a grayscale image file, interpretable by common image viewing software. Matplotlib, a popular plotting library for Python, can be used to convert a NumPy array to a grayscale image and save it directly to a file.
๐ŸŒ
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.
Discussions

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 - How to change numpy array into grayscale opencv image - Stack Overflow
How can I change numpy array into grayscale opencv image in python? After some processing I got an array with following atributes: max value is: 0.99999999988, min value is 8.269656407e-08 and type... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to convert a numpy array to greyscale image? - Stack Overflow
I am trying to convert an 8-by-8 numpy array of binary format (0 represents black and 1 represents white). This is what I run: from PIL import Image data = im.fromarray(array) # array is an 8*8 More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 13, 2022
python - how to save an array as a grayscale image with matplotlib/numpy? - Stack Overflow
I am trying to save a numpy array of dimensions 128x128 pixels into a grayscale image. I simply thought that the pyplot.imsave function would do the job but it's not, it somehow converts my array i... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-convert-an-image-to-grayscale-using-numpy-arrays-a-comprehensive-guide
How to Convert an Image to Grayscale Using NumPy Arrays: A Guide | Saturn Cloud Blog
May 1, 2026 - Color images are often represented as three-dimensional NumPy arrays, with dimensions corresponding to the height, width, and color channels of the image. Converting these to grayscale can reduce computational complexity and noise, making subsequent image processing tasks more manageable.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-show-numpy-2d-array-as-grayscale-image-in-jupyter-notebook
How to show numpy 2D array as grayscale image in Jupyter Notebook?
March 26, 2026 - To show a NumPy 2D array as a grayscale image in Jupyter Notebook, you can use Matplotlib's imshow() function with the cmap='gray' parameter.
Find elsewhere
๐ŸŒ
Experts Exchange
experts-exchange.com โ€บ questions โ€บ 29219599 โ€บ Applying-grayscale-conversion-to-list-of-numpy-array-of-images-using-Python.html
Solved: Applying grayscale conversion to list of numpy array of images using Python | Experts Exchange
July 3, 2021 - Hi, I have .npy file which is numpy version of the images. The shape of numpy image data set is images_all.shape (4750, 128, 128, 3) I need help to convert the images_all numpy array which is having colored images into grayscale images using Python?
๐ŸŒ
YouTube
youtube.com โ€บ simplecode
Python How to convert an image to grayscale with Numpy and PIL - YouTube
Simple how to in Python on turning BGR (RGB) into grayscale using basic formula of grayscaling. You can just sum pixel values and divide by 3 to achieve same...
Published: July 25, 2021
Views: 3K
๐ŸŒ
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.
๐ŸŒ
Fast.ai
forums.fast.ai โ€บ part 1 (2018)
Convert colour to grey scale numpy array - Part 1 (2018) - fast.ai Course Forums
October 13, 2018 - How do I convert an array of two colour images to an array of two gray scale images using the to_grayscale (from this site) function below. Important: I donโ€™t want image files, I want the array image_g defined below.
๐ŸŒ
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 - 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()
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 46620288 โ€บ 3d-numpy-array-to-grayscale-picture
python - 3d numpy array to grayscale picture - Stack Overflow
October 7, 2017 - meanwhile, i'm just saving the images and read them again, but i guess there is a by far more elegant way to directly store the pictures to grayscale. ... You can use your current 3D array to plot a grayscale image using matplotlib's imshow as described here.
๐ŸŒ
Saskeli
saskeli.github.io โ€บ data-analysis-with-python-summer-2019 โ€บ image_processing.html
Image processing โ€” Data analysis with Python - Summer 2019 documentation
For example, if the array contains value 255 at coordinates (0,0), then in the image the pixel at top left is white. In color images there is third axis is for the red, green, and blue components of each pixel. For each of these color components there is a value between 0.1 and 1.0 (or between 0 and 255). The combinations of different values for the three components red, green, and blue can result in at least 16.7 million colors. Since images can be represented as multidimensional arrays, we can easily process images using NumPy functions.
๐ŸŒ
Brohrer
brohrer.github.io โ€บ convert_rgb_to_grayscale.html
How to Convert an RGB Image to Grayscale - Brandon Rohrer
An intuitive way to convert a color image 3D array to a grayscale 2D array is, for each pixel, take the average of the red, green, and blue pixel values to get the grayscale value. This combines the lightness or luminance contributed by each color band into a reasonable gray approximation.