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

Pillow cannot read from array gray image?
There was an error while loading. Please reload this page More on github.com
🌐 github.com
7
April 9, 2019
python - Turning a Large Matrix into a Grayscale Image - Stack Overflow
I have a NumPy array of 3,076,568 binary values (1s and 0s). I would like to convert this to a matrix, and then to a grayscale image in Python. However, when I try to reshape the array into a 1,53... More on stackoverflow.com
🌐 stackoverflow.com
python - How to change an image to grayscale represented as a NumPy array - Stack Overflow
Preface: I have an image of a coin. This is any generic image of a coin that I plug into python, and I want to make this coin a grayscale image. The variable P represents the array containing the ... 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
🌐
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 = ...
🌐
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.
🌐
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 ...
Find elsewhere
🌐
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.
🌐
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.
🌐
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....
🌐
Towards Data Science
towardsdatascience.com › home › latest › how rgb and grayscale images are represented in numpy arrays?
How RGB and Grayscale Images Are Represented in NumPy Arrays? | Towards Data Science
January 20, 2025 - ... 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.
🌐
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).
🌐
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?