Python

How to Convert an Image to Grayscale Using Python

By ayed_amira , on 08/21/2021 - 3 minutes to read
convert an image to grayscale

Convert an image to grayscale: In this article, we will see how to grayscale an image.

Introduction

Grayscaling is a process of converting an image from different color spaces (RGB, HSV for example) into shades of gray ranging from complete black to complete white.

Greyscaling is a process often used for :

  • Dimension reduction: in RGB images there are three color channels (red, green and blue) so three dimensions, while grayscale images are unidimensional.
  • Work on other algorithms: some image processing algorithms are designed to work only on grayscale images (for example, the Canny edge detection function of the OpenCV library).
  • Reduce the complexity of the model: using the grayscale on an image allows to reduce the number of inputs in a machine learning or deep learning model.

We will see in the following tutorial what are the methods used to convert a colored image into a grayscale image.

Convert an Image to Grayscale

Convert Color Image to Grayscale using Pillow Module

The first method is to use the Pillow module to convert our images into grayscale images. The first step is to read the image that we want to modify and then perform the conversion:

from PIL import Image

img = Image.open('chameleon.jpg').convert('L')
img.save('greyscale_chameleon.jpg')

To perform the conversion, we used the convert() function combined with the ‘L’ mode ( The method has several modes you can follow this documentation for more information).

Convert an Image to Greyscale Using OpenCV Module

The second method is to use the OpenCV module. It works on the same principle as the first method:

import cv2

colored_img = cv2.imread("chameleon.jpg")
gray_img = cv2.cvtColor(colored_img.COLOR_BGR2GRAY)
cv2.imwrite("grayscale_chameleon.jpg", gray_img)

Convert an Image to Grayscale Using scikit-image Module

The color.rgb2gray() method of the scikit-image module takes an RGB image as input and returns a grayscale copy of the input image. Here is an example of its use:

from skimage import color
from skimage import io

img = io.imread('chameleon.jpg')
gray_img = color.rgb2gray(img)

Convert an Image to Grayscale Using the Conversion Formula

We can also convert an image to grayscale using the standard formula for converting RGB to grayscale, namely: 0.2989 * R + 0.5870 * G + 0.1140 * B.

We can use this formula with the library matplotlib. Here is how to proceed :

from matplotlib import pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread('chameleon.jpg')

R, G, B = img[:, :, 0], img[:, :, 1], img[:, :, 2]
gray_img = 0.2989 * R + 0.587 * G + 0.114 * B
plt.imshow(gray_img, cmap='gray')
plt.show()

Conclusion

In this tutorial, we have seen how to convert a colored image to grayscale. This conversion is essential before proceeding with any other image processing, most of the current algorithms work only on grayscale images. These methods are relatively simple to use and are very effective.

I hope that this tutorial has interested you and that the conversion of images to grayscale has no secret for you. If you have any doubt about the use of one of these methods and want to ask a question, you can leave me a comment and I will try to explain it to you as best as I can.

See you soon.

Back to Python Menu

ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

Comments

Leave a comment

Your comment will be revised by the site if needed.