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 Top answer 1 of 16
490
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()
2 of 16
101
You can also use scikit-image, which provides some functions to convert an image in ndarray, like rgb2gray.
from skimage import color
from skimage import io
img = color.rgb2gray(io.imread('image.png'))
Notes: The weights used in this conversion are calibrated for contemporary CRT phosphors: Y = 0.2125 R + 0.7154 G + 0.0721 B
Alternatively, you can read image in grayscale by:
from skimage import io
img = io.imread('image.png', as_gray=True)
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
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
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
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
Videos
02:51
Convert An Image To Grayscale Using Python - YouTube
08:46
How can I convert an RGB image into grayscale in Python - YouTube
06:42
How To Convert An Image To Grayscale In Python Using OpenCV - YouTube
05:46
How To Convert Image to Grayscale Using PIL in Python - YouTube
06:20
Convert RGB Images to Grayscale image from scratch using python ...
How can I convert an RGB image into grayscale in Python
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)
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!
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
TensorFlow
tensorflow.org › tensorflow v2.16.1 › tf.image.rgb_to_grayscale
tf.image.rgb_to_grayscale | TensorFlow v2.16.1
Converts one or more images from RGB to Grayscale.
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:
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 = ...
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 ...