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
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-grayscaling-of-images-using-opencv
Python | Grayscaling of Images using OpenCV - GeeksforGeeks
September 23, 2025 - In this method, we can directly load an image in grayscale mode by passing the flag 0 to cv2.imread(). This saves us from having to convert the image separately after loading.
Discussions

Converting RGBA image to Gray scale and then binary
Hi, I need to convert a RGBA image in to Gray scale and then to binary with a threshold but I am not able to do that in python. Here is my code . The error which I get is “AttributeError: module ‘PIL.Image’ has no attribute ‘rgb2gray’” # Import libraries from PIL import Image import ... More on discuss.python.org
🌐 discuss.python.org
6
0
August 21, 2021
Convert RGB Image to Grayscale and Display It (Python + Matplotlib) - Signal Processing Stack Exchange
I just started learning image processing and I was trying to read a RGB image then convert it to grayscale. I was hoping for something like this: However, what I get was: I tried using both scipy a... More on dsp.stackexchange.com
🌐 dsp.stackexchange.com
September 13, 2017
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
converting an image to grayscale but comes out green instead
Your code is setting: Red = Red * 0.299 Green = Red * 0.587 Blue = Blue * 0.114 Not taking them all and turning it into a final greyscale shade. The point of that equation is that the Greyscale output = 0.299 * red + 0.587 * green + 0.114 * blue - not that you just modify each channel (as you're doing). More on reddit.com
🌐 r/learnpython
3
October 2, 2025
🌐
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
January 14, 2026 - Pillow is a fork of PIL (Python Imaging Library) with a robust tool-set for image processing in Python. It provides a wide range of functions for loading, manipulating, and saving images in various formats. Here’s an example of how to convert an image to grayscale using Pillow:
🌐
scikit-image
scikit-image.org › docs › stable › auto_examples › color_exposure › plot_rgb_to_gray.html
RGB to grayscale — skimage 0.26.0 documentation
import matplotlib.pyplot as plt from skimage import data from skimage.color import rgb2gray original = data.astronaut() grayscale = rgb2gray(original) fig, axes = plt.subplots(1, 2, figsize=(8, 4)) ax = axes.ravel() ax[0].imshow(original) ax[0].set_title("Original") ax[1].imshow(grayscale, cmap=plt.cm.gray) ax[1].set_title("Grayscale") fig.tight_layout() plt.show()
🌐
Python.org
discuss.python.org › python help
Converting RGBA image to Gray scale and then binary - Python Help - Discussions on Python.org
August 21, 2021 - Hi, I need to convert a RGBA image in to Gray scale and then to binary with a threshold but I am not able to do that in python. Here is my code . The error which I get is “AttributeError: module ‘PIL.Image’ has no attribute ‘rgb2gray’” # Import libraries from PIL import Image import matplotlib.pyplot as plt import numpy as np # Reading an Image image = Image.open('Board1_1.png') image_gray = Image.rgb2gray(image)
🌐
DataFlair
data-flair.training › blogs › image-conversion-using-opencv
Image Conversion using OpenCV - Colored, Grayscale and Binary - DataFlair
March 2, 2024 - The cv2.cvtColor() function in OpenCV is specifically designed for color space conversion. Bypassing the cv2.COLOR_BGR2GRAY flag as the conversion parameter, we instruct the function to convert a BGR color image to grayscale.
🌐
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 …
Find elsewhere
🌐
Medium
ninza7.medium.com › image-processing-convert-rgb-image-into-grayscale-with-few-lines-of-python-code-b2d2745ccaea
Image Processing: Convert RGB Image into grayscale with few lines of Python Code | by Rohit Kumar Thakur | Medium
April 4, 2023 - Convert your image from RGB to Lab Color Space. ... Set the color values of the second and third channels to zeros. ... Get the grayscale image by converting the image back to RGB from Lab Color Space.
🌐
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: ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
🌐
Medium
medium.com › @bulanlobo_priestly › convert-rgb-image-to-grayscale-image-manually-without-any-library-ea92bf0e5295
Convert RGB Image to Grayscale Image Manually without Any Library | by priestly bulan lobo | Medium
November 4, 2024 - # Declare method for convert image ... = np.zeros((height, width), dtype=np.uint8) # Convert to grayscale using (R+G+B)/3 for i in range(height): for j in range(width): gray_...
🌐
Itk
examples.itk.org › src › filtering › imageintensity › convertrgbimagetograyscaleimage › documentation
Convert RGB Image to Grayscale Image — v5.4.0
Convert a RGB Image to its luminance image (grayscale one). Input image# Output image# #!/usr/bin/env python import itk import argparse parser = argparse.ArgumentParser(description="Compute RBG Image To Grayscale Image.") parser.add_argument("input_image") parser.add_argument("output_image") args = parser.parse_args() Dimension = 2 ComponentType = itk.UC InputPixelType = itk.RGBPixel[ComponentType] InputImageType = itk.Image[InputPixelType, Dimension] OutputPixelType = itk.UC OutputImageType = itk.Image[OutputPixelType, Dimension] reader = itk.ImageFileReader[InputImageType].New() reader.SetFi
🌐
Moonbooks
en.moonbooks.org › Articles › How-to-convert-an-image-to-grayscale-using-python-
How to convert an image to grayscale using python ?
Note: the conversion to grayscale is not unique see l'article de wikipedia's article). It is also possible to convert an image to grayscale and change the relative weights on RGB colors, example:
🌐
MLK
machinelearningknowledge.ai › home › 8 ways to convert image to grayscale in python using skimage, pillow, opencv, numpy, matplotlib, imageio
8 Ways to Convert Image to Grayscale in Python using Skimage, Pillow, OpenCV, Numpy, Matplotlib, ImageIO - MLK - Machine Learning Knowledge
January 18, 2024 - Scikit Image or Skimage is a Python based open-source package for various image processing algorithms. Any color image can be converted to grayscale with the help of color.rgb2gray() function of Skimage.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-an-image-to-grayscale-in-pytorch
How to convert an image to grayscale in PyTorch - GeeksforGeeks
July 23, 2025 - If the input image is torch Tensor then it is expected to have [3, H, W] shape, H, W is height and width respectively. The below syntax is used to convert an image to grayscale. Pytorch is an open-source deep learning framework available with a Python and C++ interface.
🌐
TutorialsPoint
tutorialspoint.com › pytorch-how-to-convert-an-image-to-grayscale
PyTorch – How to convert an image to grayscale?
January 6, 2022 - # import required libraries import torch import torchvision.transforms as transforms from PIL import Image # Read the image img = Image.open('laptop.jpg') # define a transform to convert the image to grayscale transform = transforms.Grayscale() # apply the above transform on the image img = ...
🌐
Quora
quora.com › How-do-I-convert-a-color-image-to-gray-scale-in-OpenCV-with-Python
How to convert a color image to gray scale in OpenCV with Python - Quora
Answer: There are a couple ways of doing this, but this is a very basic way to convert. If you wish to have the HSV format and keep the 3 channels to modify hue/sat/brightness, then you will just need to change the 2nd parameter to the proper conversion. Below you will find my example of how to ...
🌐
Jeremy Morgan
jeremymorgan.com › tutorials › opencv › how-to-grayscale-image
How to Grayscale an Image
March 23, 2023 - And there you have it! You’ve successfully grayscaled an image using OpenCV and Python. Grayscaling converts a colorful image into a monochrome representation by assigning each pixel a single intensity value.
🌐
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
January 14, 2024 - One common task is converting color images to grayscale, which can simplify subsequent analyses. Today, we’ll explore how to accomplish this using NumPy arrays. ... NumPy, short for Numerical Python, is a powerful library that supports large, multi-dimensional arrays and matrices, along with ...