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
🌐
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 - We’ll use Image.fromarray() again, but with a small twist—this time, we’ll specify the mode as 'L'. The 'L' mode tells PIL to treat the image as grayscale. from PIL import Image # Convert grayscale array to a PIL Image grayscale_img = ...
🌐
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?
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()
🌐
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 ... channels of the image. Converting these to grayscale can reduce computational complexity and noise, making subsequent image processing tasks more manageable....
🌐
GitHub
github.com › python-pillow › Pillow › issues › 3781
Pillow cannot read from array gray image? · Issue #3781 · python-pillow/Pillow
April 9, 2019 - I have a pic: Do some conversion: AB = Image.open("./391.jpg").convert('RGB') arr=np.array(AB) gray=arr@np.array([0.2125, 0.7154, 0.0721]) # gray scale now Whatever mode you use, it just looks like fucked up: But if you use scipy.misc.to...
Author: python-pillow
🌐
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.
🌐
Codecademy
codecademy.com › docs › python:pillow › image module › fromarray()
Python:Pillow | Image Module | fromarray() | Codecademy
June 8, 2024 - RGB: It represents the color palette made from Red, Green, and Blue colors. It has 3x8-bit pixels available to store the values. L: It represents grayscale or shade of gray from white to black.
Find elsewhere
🌐
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 - 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. The imshow() function displays the image based on the array values and the savefig() function saves ...
🌐
Pythoninformer
pythoninformer.com › python-libraries › numpy › numpy-and-images
PythonInformer - Image processing with pillow and NumPy
October 2, 2022 - We will see later that if there are 4 channels an RGBA image is created, and if there is only 1 channel a greyscale image is created. These are defaults, it is possible to use different colour spaces using the fromarray mode parameter, see the PIL documentation.
🌐
Pillow Documentation
pillow.readthedocs.io › en › stable › reference › Image.html
Image module - Pillow (PIL Fork) 12.3.0 documentation
Configures the image file loader so it returns a version of the image that as closely as possible matches the given mode and size. For example, you can use this method to convert a color JPEG to grayscale while loading it.
🌐
Saskeli
saskeli.github.io › data-analysis-with-python-summer-2019 › image_processing.html
Image processing — Data analysis with Python - Summer 2019 documentation
def fadex(image): height, width ... ... This exercise can give two points at maximum! ... Write a function to_grayscale that takes an RGB image (three dimensional array) and returns a two dimensional gray-scale image....
🌐
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.
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
Image processing with Python, NumPy | note.nkmk.me
October 20, 2020 - pil_img = Image.fromarray(im) print(pil_img.mode) # RGB pil_img.save('data/temp/lena_save_pillow.jpg') ... A grayscale image (2D array) can also be passed to Image.fromarray(). mode automatically becomes 'L' (grayscale).
🌐
Towards Data Science
towardsdatascience.com › home › artificial intelligence › deep learning › how rgb and grayscale images are represented in numpy arrays?
How RGB and Grayscale Images Are Represented in NumPy Arrays? | Towards Data Science
December 4, 2021 - ... A single grayscale image can be represented using a two-dimensional (2D) NumPy array or a tensor. Since there is only one channel in a grayscale image, we don't need an extra dimension to represent the color channel.
🌐
scikit-image
scikit-image.org › skimage-tutorials › lectures › 00_images_are_arrays.html
Images are numpy arrays — Image analysis in Python
Use Python 3.5’s matrix multiplication, @, to convert an RGB image to a grayscale luminance image according to the formula above.
🌐
Quora
quora.com › How-do-I-plot-a-grayscale-image-with-a-2D-array-of-random-numbers-in-Python
How to plot a grayscale image with a 2D array of random numbers in Python - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.