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
🌐
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 - It’s a fundamental package for scientific computing with Python. Color images are often represented as three-dimensional NumPy arrays, with dimensions corresponding to the height, width, and color channels of the image. Converting these to grayscale can reduce computational complexity and noise, making subsequent image processing tasks more manageable.
Discussions

python - Converting an image to grayscale using numpy - Stack Overflow
I have an image represented by a numpy.array matrix nxm of triples (r,g,b) and I want to convert it into grayscale, , using my own function. My attempts fail converting the matrix nxmx3 to a matri... More on stackoverflow.com
🌐 stackoverflow.com
July 11, 2018
numpy - Turn a image to grayscale in python - Stack Overflow
I'm a newbie to tensorflow and keras, and I'm trying to create a CNN model for The Street View House Numbers (SVHN) dataset. The dataset contains color images, and I want to turn them in grayscale... More on stackoverflow.com
🌐 stackoverflow.com
python - How to make grayscale image using numpy and matplotlib - Stack Overflow
If the end goal is just to save the image out as a grayscale version then Pillow will do the job. If the goal is to send the grayscale version to some other part of the script where numpy/matplotlib is required you can either use the second part of the answer at the above link or convert the Pillow ... 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
🌐
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
🌐
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.
🌐
AskPython
askpython.com › python-modules › matplotlib › convert-rgb-to-grayscale
Convert an RGB image into grayscale using Matplotlib - AskPython
February 15, 2023 - We will use the numpy.dot() function and create a function to convert the above image into a grayscale image.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-grayscaling-of-images-using-opencv
Python | Grayscaling of Images using OpenCV - GeeksforGeeks
September 23, 2025 - DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 23 Sep, 2025 · Grayscaling is the process of converting an image from other color spaces e.g. RGB, CMYK, HSV, etc. to shades of gray. It varies between complete black and complete white.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 63900600 › turn-a-image-to-grayscale-in-python
numpy - Turn a image to grayscale in python - Stack Overflow
Is there any way to actually turn this image to grayscale? (I do not know how to process an image in this kind of programming languages. If this is a dumb question, please forgive me and provide a brief explain.) I provided images and code below, I'll be grateful for any help. ... Copyimport matplotlib import matplotlib.pyplot as plt import numpy as np #Read picture: picture = plt.imread('google.jpg') print("google logo's shape is: ",picture.shape) #(500, 500, 3) #saving picture as an np array: pic_array = np.array(picture) #Turning image to grayscale grayscale_pic = np.expand_dims(np.dot(pic_array[...,:3],[0.299, 0.587, 0.144]),axis = 0) #Dimensions shifted, (probly my mistake): grayscale_pic = np.moveaxis(grayscale_pic, 0, -1) print("shape of grayscale pic = ", grayscale_pic.shape) # (500, 500, 1) plt.imshow(picture) #Figure_1 plt.show() plt.imshow(grayscale_pic) #Figure_2 plt.show()
🌐
Codepointtech
codepointtech.com › home › grayscale image processing with numpy: a python guide
Grayscale Image Processing with NumPy: A Python Guide - codepointtech.com
July 4, 2026 - The formula is: Grayscale = 0.2989 * R + 0.5870 * G + 0.1140 * B · # Ensure the image is in RGB format before conversion if img.mode != "RGB": img = img.convert("RGB") img_array = np.array(img) # Extract R, G, B channels R, G, B = img_array[:, :, 0], img_array[:, :, 1], img_array[:, :, 2] ...
🌐
Muthu
muthu.co › converting-color-images-to-grayscale-using-numpy-and-some-mathematics
Converting Color Images to Grayscale using numpy and some Mathematics – Muthukrishnan
You can use the standard skimage method to perform the same grayscale conversion. from skimage.color import rgb2gray img_gray = rgb2gray(orig_img) The algorithm used within rgb2gray is the luminosity method. References: https://docs.gimp.org/2.6/en/gimp-tool-desaturate.html http://poynton....
🌐
Stack Overflow
stackoverflow.com › questions › 71566658 › how-to-make-grayscale-image-using-numpy-and-matplotlib › 71567012
python - How to make grayscale image using numpy and matplotlib - Stack Overflow
I want to make a grayscale image but getting errors like this, I have already searched on google but don't get the answer and I don't know what keywords I should use to search it on google. ... Copy!pip install matplotlib import numpy as np import matplotlib.pyplot as plt img = plt.imread('/content/drive/MyDrive/Colab Notebooks/gambar.jpg') img = np.zeros([120, 300, 3], dtype=np.uint8) h, w = img[:2] for x in range(w): for y in range(h): gray = (20 + 125 + 255) / 3 img[y,x] = gray plt.imshow(img) plt.show()
🌐
Google Groups
groups.google.com › g › scikit-image › c › y7GdVM7xd7Q
Converting a numpy array to grayscale
Here is a snippet of code: >>> gray = ndimage.imread('1.jpg',flatten=True) >>> gray /= gray.max() >>> gray array([[ 0.30133331, 0.2895686 , 0.28172547, ... ... >>> gray2 = color.rgb2gray(rgb) >>> gray2 array([[ 0.31065608, 0.29889137, 0.29104824, ..., ... I believe this difference is causing ...
🌐
Finxter
blog.finxter.com › home › learn python blog › how to convert an image from rgb to grayscale in python
How to Convert an Image from RGB to Grayscale in Python - Be on the Right Side of Change
July 16, 2022 - Above, two (2) libraries and called in: NumPy to use the np.dot() function and Matplotlib to handle the other calls. Next, a function is created accepting an object as an argument.
🌐
GitHub
github.com › deenaariff › GrayScale
GitHub - deenaariff/GrayScale: A small python script to convert images to greyscales by averaging RGB values per pixel. · GitHub
Python script using matplotlib and numpy to convert any image to grayscale through averaging RGB values in a three dimensional numpy-array of the image.
Starred by 11 users
Forked by 4 users
Languages: Python
🌐
Stack Overflow
stackoverflow.com › questions › 66518986 › converting-image-to-grayscale-python
numpy - Converting image to grayscale python - Stack Overflow
September 26, 2021 - Can anyone help with the code to go after for loop to convert to grayscale either by average method or weighted method · import matplotlib.pyplot import numpy myImage = matplotlib.pyplot.imread('flower.png') height=myImage.shape[0] width=myImage.shape[1] for x in range(0, height-1): for y ...
🌐
Reddit
reddit.com › r/learnpython › convering numpy getdata() rgb values to grayscale
r/learnpython on Reddit: Convering numpy getdata() RGB values to grayscale
January 23, 2018 -

I am trying to use getdata() on a 500x500 image. As far as i understand, this returns a 750000 entry array in groups of three corresponding to the RGB values of each cell. I want to change these to a single value (in grayscale) and arrange it in a 500x500 matrix to mimic the image. Here is my code

import sys
import numpy as np
from descent import descent_func
from PIL import Image

myimage = Image.open("Spiral.png")
#myimage.show()

b = np.array(list(myimage.getdata()))
b = b.reshape(1,750000)
c = np.empty((500,500))

k = 0

for i in range(0,499):
    for j in range(0,499):
        c[i][j] = b[0][k]
        k = k + 3
        
print(c)
img = Image.fromarray(c)
img.show()

I figured since my input drawing in strictly black and white, i could just use the R value from the RGB for my greyscale value, but this produces a shifted version of the input image. I'm very new to python so not quite sure how to proceed. Thanks!

🌐
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 - This straightforward code snippet uses OpenCV to first define a 2D NumPy array with random pixel values. The array is then written to an image file in PNG format using the cv2.imwrite() function.
🌐
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_...
🌐
Delft Stack
delftstack.com › home › howto › python › convert image to grayscale python
How to Convert Image to Grayscale in Python | Delft Stack
February 2, 2024 - Python 3 Basic Tkinter Python Modules ... Pi ... Convert an Image to Grayscale in Python Using the color.rgb2gray() Method of the scikit-image Module...