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
🌐
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
Discussions

python - Use PIL to convert a grayscale image to a (1, H, W) numpy array - Stack Overflow
Using PIL to convert a RGB image to a (H, W, 3) numpy array is very fast. ... However, I cannot find a fast way to convert a grayscale image to a (H, W, 1) array. More on stackoverflow.com
🌐 stackoverflow.com
python - Grayscale image to NumPy array for Fourier transform - Stack Overflow
Currently I'm using PIL and NumPy. I have a colored png image and I want to: Read it in in grayscale Convert to NumPy array Perform a FFT on array Display the image This is what I'm trying (in IPyt... More on stackoverflow.com
🌐 stackoverflow.com
image - Read the picture as a grayscale numpy array, and save it back - Stack Overflow
When you are creating the numpy array using the image data from your Pillow object, be advised that the default precision of the array is int32. I'm assuming that your data is actually uint8 as most images seen in practice are this way. Therefore, you must explicitly ensure that the array is ... More on stackoverflow.com
🌐 stackoverflow.com
August 23, 2016
Grayscale images (as numpy arrays) display as blue for values of exactly -1?
I am using a grayscale image as a numpy array as the “image” keyword argument for the ImageStim constructor. I use PIL to read the images and convert them to grayscale (0 - 255), but I keep getting a warning that says: “WARNING : numpy arrays used as textures must be in the range of -1:1” ... More on discourse.psychopy.org
🌐 discourse.psychopy.org
0
0
October 6, 2020
People also ask

How do I convert a PIL image to a NumPy array?
Open the image with Pillow and call np.asarray(image) or np.array(image) to obtain array data.
🌐
pythonpool.com
pythonpool.com › home › numpy › convert a pil image to a numpy array: modes, shape, and dtype
Convert a PIL Image to a NumPy Array: Modes, Shape, and Dtype
How do I convert a NumPy array back to a PIL image?
Use Image.fromarray(array) after ensuring the array dtype, shape, and value range match the desired Pillow mode.
🌐
pythonpool.com
pythonpool.com › home › numpy › convert a pil image to a numpy array: modes, shape, and dtype
Convert a PIL Image to a NumPy Array: Modes, Shape, and Dtype
What is the difference between np.asarray() and np.array()?
asarray() avoids a copy when possible, while np.array() generally creates a new array unless copy behavior is changed explicitly.
🌐
pythonpool.com
pythonpool.com › home › numpy › convert a pil image to a numpy array: modes, shape, and dtype
Convert a PIL Image to a NumPy Array: Modes, Shape, and Dtype
🌐
DataCamp
datacamp.com › doc › numpy › converting-images-to-numpy
Converting Images to NumPy
from PIL import Image import numpy as np image = Image.open('example.jpg').convert('L') image_array = np.array(image) print(image_array) In this example, the image is converted to grayscale using `.convert('L')` before converting it to a NumPy array, then printed.
🌐
pythontutorials
pythontutorials.net › blog › pil-image-to-numpy-array
Converting PIL Images to NumPy Arrays: A Comprehensive Guide — pythontutorials.net
Here, we use the convert('L') method to ensure the image is in grayscale mode before converting it to a NumPy array. Before converting an image to a NumPy array, you may want to resize it to a specific size.
🌐
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.
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 - The output is a PNG file named ‘grayscale_image_pil.png’. In this code example, a 2D NumPy array with random values is first created, then Pillow’s Image.fromarray() function is used to transform this array into an image.
🌐
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.
🌐
Python Pool
pythonpool.com › home › numpy › convert a pil image to a numpy array: modes, shape, and dtype
Convert a PIL Image to a NumPy Array: Modes, Shape, and Dtype
July 13, 2026 - Convert Pillow images to NumPy arrays with np.asarray(), inspect RGB and RGBA modes, copy safely, and convert arrays back to images.
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-108.php
Python NumPy: Convert a PIL Image into a NumPy array - w3resource
August 29, 2025 - Write a program to load a PIL image and convert it into a NumPy array, then verify the array’s shape. Create a function that transforms a PIL image into grayscale and returns the corresponding NumPy array.
🌐
GitHub
gist.github.com › panzi › 1ceac1cb30bb6b3450aa5227c02eedd3
Python: Convert PIL.Image to OpenCV BGR(A)/grayscale image (NumPy array). This supports the most common image modes, but there are more! Patches for those are welcome. · GitHub
Python: Convert PIL.Image to OpenCV BGR(A)/grayscale image (NumPy array). This supports the most common image modes, but there are more! Patches for those are welcome. - pil2cv.py
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
Image processing with Python, NumPy | note.nkmk.me
October 20, 2020 - Convert BGR and RGB with Python, OpenCV (cvtColor) If you convert the image to grayscale with convert('L') and then pass it to np.array(), it returns 2D ndarray whose shape is (row (height), column (width)).
🌐
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.
🌐
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 - import matplotlib.image as mpimg import numpy as np import matplotlib.pyplot as plt · Next, we load the image using mpimg.imread(). This function reads an image from a file into an array. ... It’s important to understand the structure of the image data. A color image is typically represented as a 3D array, where the third dimension represents color channels (Red, Green, Blue). ... Now, we’ll convert the image to grayscale.
🌐
Pillow Documentation
pillow.readthedocs.io › en › stable › reference › Image.html
Image module - Pillow (PIL Fork) 12.3.0 documentation
... When converting Pillow images to arrays however, only pixel values are transferred. This means that P and PA mode images will lose their palette. ... Optional mode to use when reading obj.
🌐
PsychoPy
discourse.psychopy.org › coding
Grayscale images (as numpy arrays) display as blue for values of exactly -1? - Coding - PsychoPy
October 6, 2020 - I am using a grayscale image as a numpy array as the “image” keyword argument for the ImageStim constructor. I use PIL to read the images and convert them to grayscale (0 - 255), but I keep getting a warning that says: “WARNING : numpy arrays used as textures must be in the range of -1:1” ...
🌐
TutorialsPoint
tutorialspoint.com › python_pillow › python_pillow_m_l_with_numpy.htm
Python Pillow - M L with Numpy
Pixel values of image at (150, 150) of the grayscale image is: 250 · The numpy.asarray() function can be used to convert a Pillow image to a NumPy array. However, it's important to note that when converting Pillow images to arrays, only the ...
🌐
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()