RGB to Grayscale Image Conversion in Python

In this instructional exercise, we will show you various manners by which you can change over any picture into Grayscale in Python by utilizing various libraries like scikit-image Pillow, and OpenCV. Every one of the manners in which will be displayed with models for simple comprehension.

Ways to Convert Image to Grayscale in Python

For every one of the models, the underneath canine picture will be utilized as info.

1. With Skimage (Scikit Image) – color.rgb2gray()

An open-source toolkit for numerous image processing techniques, Scikit Image or Skimage is based on Python. With the aid of Skimage’s color.rgb2gray() method, any color picture may be converted to grayscale.

The code will be:

#import the necesaary modules
from skimage import color
from skimage import io

image = io.imread('pexels-bhavitya-indora-3224533.jpg')
imageGray = color.rgb2gray(image)



2. With Pillow (PIL) – convert()

Another Python image processing package called Pillow has a method called img.convert() that may be used to convert an image to grayscale.


from PIL import Image

imag = Image.open('pexels-bhavitya-indora-3224533.jpg')
imageGray = imag.convert('L')


3. With OpenCV – cv2.imread()

There are several techniques to convert a picture to grayscale, with OpenCV being the most well-known image processing program available. The image might be switched over completely to grayscale in this first technique by providing the banner worth as 0 and the pictures record name to the capability cv2.imread() while perusing the picture.


import cv2

# Convert to Grayscale

# To display Image
windname='RGB to GRAYSCALE'
cv2.namedWindow(windname, cv2.WINDOW_NORMAL)


So in this, we can convert RGB to Grayscale image conversion in Python.

I hope you like this article.


Leave a Reply

Your email address will not be published. Required fields are marked *