How about doing it with Pillow:

from PIL import Image
img = Image.open('image.png').convert('L')
img.save('greyscale.png')

If an alpha (transparency) channel is present in the input image and should be preserved, use mode LA:

img = Image.open('image.png').convert('LA')

Using matplotlib and the formula

Y' = 0.2989 R + 0.5870 G + 0.1140 B 

you could do:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])

img = mpimg.imread('image.png')     
gray = rgb2gray(img)    
plt.imshow(gray, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)
plt.show()
Answer from unutbu on Stack Overflow
🌐
Python Examples
pythonexamples.org › pillow-convert-image-to-grayscale
Convert Image to Grayscale in Pillow
To convert given image to grayscale using Pillow library, you can use Image.convert() function.
Discussions

Incorrect Grayscale Conversion
What did you do? I'm having a weird inconsistency in the way Pillow is treating grayscale images, and the difference is enough to throw off some metrics I need to compute. After a lot of debugg... More on github.com
🌐 github.com
10
April 18, 2019
Converting this 16-bit grayscale image to 'L' mode destroys it
Here is a 16-bit grayscale image: It clearly contains a gradient of grays. If I open it with Pillow, convert it to 8-bit grayscale, and save it, like so... >>> from PIL import Image >&g... More on github.com
🌐 github.com
9
February 20, 2018
Convert image to grayscale with pillow
🌐 r/learnpython
3
1
July 31, 2016
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
🌐
Codecademy
codecademy.com › docs › python:pillow › image module › .convert()
Python:Pillow | Image Module | .convert() | Codecademy
March 21, 2025 - In this example, the .convert() method uses the L mode to convert an image to grayscale:
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pil-imageops-greyscale-method
Python PIL | ImageOps.grayscale() method - GeeksforGeeks
October 27, 2021 - Syntax: PIL.ImageOps.grayscale(image) Parameters: image – The image to convert into grayscale.
🌐
Pillow Documentation
pillow.readthedocs.io › en › stable › reference › Image.html
Image module - Pillow (PIL Fork) 12.2.0 documentation
This supports all possible conversions between “L”, “RGB” and “CMYK”. The matrix argument only supports “L” and “RGB”. When translating a color image to grayscale (mode “L”), the library uses the ITU-R 601-2 luma transform:
🌐
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 - By iterating through each pixel you can convert 24-bit to 8-bit or 3 channel to 1 channel for each pixel by using the formula above. You can read the original ITU-R Recommendation 601 7th edition. Alternatively, you can try Rec. 709 · L = R * 2125/10000 + G * 7154/10000 + B * 0721/10000 · You can read the original ITU-R Recommendation 709 6th edition. This is by far the most common method to acquire monochrome (B&W and or grayscale) images.
Find elsewhere
🌐
YouTube
youtube.com › learn python
How To Convert Image to Grayscale Using PIL in Python - YouTube
#PIL #PIllow #Python #Grayscaleimage #Imageprocessing #OpenCV
Published   December 12, 2023
Views   524
🌐
Cloudinary
cloudinary.com › home › a guide to converting images to grayscale with python introduction
A Guide to Converting Images to Grayscale with Python Introduction | Cloudinary
April 21, 2024 - In my experience, here are tips that can help you better implement and optimize image grayscale conversions using Python and Cloudinary: Batch process with Pillow for efficient grayscale conversion When working with a large number of images, use Pillow’s ImageEnhance module along with multiprocessing to batch-process grayscale conversions.
🌐
GitHub
github.com › python-pillow › Pillow › issues › 3800
Incorrect Grayscale Conversion · Issue #3800 · python-pillow/Pillow
April 18, 2019 - It is very hard to detect this difference by just looking at the grayscale ouputs but I am including them for completeness. The difference becomes extremely apparent under aggressive JPEG compression. ... im = np.asarray(Image.open(args.input).convert('L')) im2 = cv2.cvtColor(cv2.imread(args.input), cv2.COLOR_BGR2GRAY) diff = im - im2 cv2.imwrite('pillow_output.png', im) cv2.imwrite('opencv_output.png', im2) cv2.imwrite('diff.png', diff)
Author   Queuecumber
🌐
GitHub
github.com › python-pillow › Pillow › issues › 3011
Converting this 16-bit grayscale image to 'L' mode destroys it · Issue #3011 · python-pillow/Pillow
February 20, 2018 - Here is a 16-bit grayscale image: It clearly contains a gradient of grays. If I open it with Pillow, convert it to 8-bit grayscale, and save it, like so... >>> from PIL import Image >>> test_img = Image.open('test.png') >>> test_img.mode...
Author   ExplodingCabbage
🌐
Reddit
reddit.com › r/learnpython › convert image to grayscale with pillow
r/learnpython on Reddit: Convert image to grayscale with pillow
July 31, 2016 -

Hi all,

I've made this simple class to create a grayscale image:

from PIL import Image, ImageFilter
from numpy import array

class ScaleUtils():
    def __init__(self, p, h=28, w=28):
        self.im = Image.open(p)
        self.size = (h,w)
    def toGrey(self):
        self.im = self.im.convert("L")
        return self
    def resize(self):
        self.im = self.im.resize(self.size)
        return self
    def getArray(self):
        return array(self.im)
    def getImage(self):
        return self.im
    def run(self):
        return self.toGrey().resize().getArray()

if __name__ == "__main__":
    #ScaleUtils("test.jpg").toGrey().resize().getImage().save("output.jpg", "JPEG")
    print(ScaleUtils("test.jpg").toGrey().resize().getArray())

Can I be certain that this will produce everytime an array with values between 0-255 for every given image? Thanks

🌐
Medium
medium.com › @nutanbhogendrasharma › convert-a-color-image-to-gray-scale-in-python-7fe952934c0a
Convert a Color Image to Gray Scale in Python | by Nutan | Medium
July 27, 2023 - Convert a Color Image to Gray Scale in Python In this blog, we will convert color images into grayscale using the Pillow and CV2 packages. Installation We will use opencv-python and the pillow module …
🌐
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?

🌐
GitHub
github.com › python-pillow › Pillow › issues › 1979
Converting palette mode to grayscale+alpha deforms image · Issue #1979 · python-pillow/Pillow
What did you do? Convert a png/gif image in "P" (palette) mode to "LA" (grayscale + alpha). from PIL import Image image = Image.open('palette_image.png') print('original mode: %s' % image.mode) # converting straight to grayscale+alpha le...
Author   ghost
🌐
Medium
medium.com › @jamalhussainshah › grayscale-to-rgb-image-converter-6606aeb42296
Grayscale to RGB Image Converter. This Python script utilizes the Pillow… | by Jamal Shah | Medium
January 24, 2024 - from PIL import Image import os def convert_grayscale_to_rgb(input_path, output_path): # Open the grayscale image grayscale_image = Image.open(input_path).convert("L") # Convert to RGB rgb_image = Image.merge("RGB", (grayscale_image, grayscale_image, grayscale_image)) # Save the RGB image rgb_image.save(output_path) def process_images_in_directory(src_directory, dest_directory): # Loop through all files in the source directory and its subdirectories for root, dirs, files in os.walk(src_directory): for file in files: # Check if the file is an image (you can adjust the condition based on your fi
🌐
TutorialsPoint
tutorialspoint.com › python_pillow › python_pillow_converting_color_string_to_grayscale_values.htm
Python Pillow - Converting color string to Grayscale values
In this example we use ImageColor.getcolor() to retrieve the RGB value of the color blue and the RGBA value of the color green in RGBA mode. We can adjust the color and mode parameters to suit our specific requirements.
🌐
Brandonrohrer
brandonrohrer.com › convert_rgb_to_grayscale
How to Convert an RGB Image to Grayscale - Brandon Rohrer
By the way, all the interesting information in this post all comes from the Wikipedia entry on Grayscale. (If you find it helpful, maybe send them a dollar.) The code we're working from loads jpeg images for an autoencoder to use as inputs. This is accomplished with using Pillow and Numpy: from PIL import Image import numpy as np color_img = np.asarray(Image.open(img_filename)) / 255 · This reads the image in and converts it into a Numpy array.
🌐
YouTube
youtube.com › shorts › aKqUwaJtsho
EASY way to convert images to grayscale using Python Pillow libray #python #pillow #shorts - YouTube
In this video, we'll show you an easy way to convert images to grayscale using the Python Pillow library. This process is simple and can be used to convert a...
Published   March 17, 2023