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
Discussions

matplotlib - How can I convert an RGB image into grayscale in Python? - Stack Overflow
The basic steps you need to do ... image. matplotlib does not appear to provide a mechanism to convert to YUV/YIQ, but it does let you convert to HSV. Try using matplotlib.colors.rgb_to_hsv(img) then slicing the last value (V) from the array for your grayscale. It's not quite the same as a luma value, but it means you can do it all in matplotlib. ... Alternatively, you could use PIL or the builtin ... More on stackoverflow.com
🌐 stackoverflow.com
How do I convert from grayscale to black and white?
In Python the PIL images directly support converting to grayscale or pure black and white. Have a Look at the stackoverflow link below. The first answer uses a default transformation to black and white and the 2nd answer uses a custom threshold https://stackoverflow.com/questions/9506841/using-pil-to-turn-a-rgb-image-into-a-pure-black-and-white-image More on reddit.com
🌐 r/learnpython
5
1
September 24, 2023
python - Turning a Large Matrix into a Grayscale Image - Stack Overflow
Furthermore, how would I turn it into a grayscale image? Any help or advice would be appreciated. Thank you. ... Er... 3,076,568 values would be a ~1754x1754 image, not a 1,538,284x1,538,284 image. ... Save this answer. ... Show activity on this post. Your array of "binary values" is an array of bytes? If so, you can do (using Pillow) after resizing it: Copyfrom PIL import Image im = Image.fromarray... More on stackoverflow.com
🌐 stackoverflow.com
October 8, 2011
python - PIL load RGB matrix of a grayscale image - Stack Overflow
However, if what you want is to create a RGB image from your grayscale image, its quite easy, just repeat the matrix 3 times, creating a NxMx3 np.array ... Oh. Thanks! How can I do that? ... I am lookign for it, but first: why do you want to do this? It may be better to continue using it as grayscale ... Actually, I'm passing some images through a trained VGG model(Convolutional Neural Network) and it accepts only RGB pictures :D ... I am not proficien in PIL... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
github.com › python-pillow › Pillow › issues › 3781
Pillow cannot read from array gray image? · Issue #3781 · python-pillow/Pillow
April 9, 2019 - Pillow cannot read from array gray image?#3781 · Copy link · Labels · Conversion · zjplab · opened · on Apr 9, 2019 · Issue body actions · 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 ·
Author: python-pillow
🌐
Pillow Documentation
pillow.readthedocs.io › en › stable › reference › Image.html
Image module - Pillow (PIL Fork) 12.3.0 documentation
Since pixel values do not contain information about palettes or color spaces, this can be used to place grayscale L mode data within a P mode image, or read RGB data as YCbCr for example. See: Modes for general information about modes. ... An image object. Added in version 1.1.6. PIL.Image.fromarrow(obj: SupportsArrowArrayInterface, mode: str, size: tuple[int, int]) → Image[source]¶ · Creates an image with zero-copy shared memory from an object exporting the arrow_c_array interface protocol:
🌐
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 Python Imaging Library (PIL), known as Pillow in its current form, provides the Image module that can convert NumPy arrays to image objects. The fromarray() function is particularly useful here, as it can directly handle NumPy arrays and ...
🌐
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 - A 3D array with three channels (height, width, 3) is required for RGB images (mode='RGB'). Here’s a quick way to check and fix the shape of your array: print(data.shape) # Make sure it matches (height, width) for grayscale or (height, width, 3) for RGB · 2. Incorrect data types PIL expects pixel values in the uint8 format (numbers between 0 and 255).
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python matplotlib howto's" › "how to convert a numpy array to pil image in python"
How to Convert a NumPy Array to PIL Image in Python | Delft Stack
March 13, 2025 - Here’s how to handle grayscale ... gray_array.astype(np.uint8) # Convert the 2D NumPy array to a PIL image gray_image = Image.fromarray(gray_array, mode='L') # Display the grayscale image gray_image.show()...
🌐
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.
Find elsewhere
🌐
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
🌐
Codecademy
codecademy.com › docs › python:pillow › image module › fromarray()
Python:Pillow | Image Module | fromarray() | Codecademy
June 8, 2024 - L: It represents grayscale or shade of gray from white to black. It only has an 8-bit pixel to store its value. The example below shows the value of a pixel in coordinate (0,0) after the parameter mode is used with CMYK as the value. # Import Image module from Pillow library · from PIL import Image · import numpy as np · # Defined an array 300 by 36 with 200 as all the pixel values ·
🌐
GeeksforGeeks
geeksforgeeks.org › python-pil-imageops-greyscale-method
Python PIL | ImageOps.grayscale() method - GeeksforGeeks
October 27, 2021 - The ImageOps module contains a number of ‘ready-made’ image processing operations. This module is somewhat experimental, and most operators only work on L and RGB images. ImageOps.grayscale() Convert the image to grayscale.
🌐
Reddit
reddit.com › r/learnpython › how do i convert from grayscale to black and white?
r/learnpython on Reddit: How do I convert from grayscale to black and white?
September 24, 2023 -

Consider the following image:

Image

If you zoom in, you will observe that the picture contains pixels of the form (0,0,0,x) in RGBA format (I have used pillow to verify this btw). What I want to do is if the pixel's alpha is less than equal to 200, then write (0,0,0,0) at the pixel's place, and if it is greater than 200 then write (0,0,0,255) at the pixel's place.

Here's my code rn:

from PIL import Image
import numpy as np

def inp_gen(img_path):
    im = Image.open(img_path, 'r')
    pix_vals = list(im.getdata())
    layer = [x[3] for x in pix_vals]
    for j in layer:
        if j <= 200:
            layer[layer.index(j)] = 0
        if j > 200:
            layer[layer.index(j)] = 1 
    return layer

def render_img(img_data):
    image_data = [(0,0,0,255*x) for x in img_data]
    data = np.array(image_data, dtype=np.uint8).T
    image = Image.fromarray(data)
    image.show() 

render_img(img_gen("Path to image in drive link on my PC"))

Sadly, this does not give me the output I want:

Output

So what should I do now?

🌐
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).
🌐
TutorialsPoint
tutorialspoint.com › python_pillow › python_pillow_imageops_grayscale_function.htm
Python Pillow - ImageOps.grayscale() Function
The mode attribute will indicate "RGB" or "RGBA" for color images and "L" for grayscale images. from PIL import Image, ImageOps import numpy as np # Open an RGB image file input_image = Image.open("Images/tutorialspoint2.jpg") # Open an RGBA ...
🌐
HolyPython
holypython.com › home › python pil tutorial › how to convert an image to black & white in python (pil)
How to convert an image to Black & White in Python (PIL) | HolyPython.com
December 10, 2022 - We are going to show you how to achieve grayscale or black and white images quickly and practically using Python’s PIL (pillow) image processing library’s Image and ImageEnhance modules. ... Let’s first import the Python libraries and modules we will need. Let’s also get a colorful image ready to work on it. from PIL import Image, ImageEnhance file = "C://Users/ABC/20.jpg" img = Image.open(file) img.show()
🌐
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
🌐
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.