Image Processing: Convert RGB Image into grayscale with few lines of Python Code

Image processing project of converting RGB image into grayscale using Lab Color Space

Rohit Kumar Thakur
3 min readJul 17, 2021
Photo by Hitesh Choudhary on Unsplash

When you step into the image processing field, you always try to play with different images and their outcomes with different types of machine learning algorithms. But if you are a beginner then you have to deal with RGB Images and grayscale first. Let’s find out what is this and how to convert RGB images into grayscale.

What is an RGB image?

As you probably know, RGB stands for Red, Green, and Blue. The RGB image is a composite of three different grayscale images that correspond to the intensity of Red, Green, and Blue color. These three grayscale images are recombined into a single image, which humans perceive as colored images. By combining different intensities of Red, Green, and Blue in grayscale, you can get a wide range of colored RGB images.

All the images are represented in RGB because, in colored images, each pixel can be represented as the vector of three numbers for the three primary colors: red, green, and blue. Each of these numbers is ranging from 0 to 255.

What is a Grayscale image?

It is simply one in which the colors are the shades of gray. You can get different shades of gray ranging from 0 to 255 (black to white). It varies according to the given intensities.

Attention all developers seeking to make social connections and establish themselves while earning passive income — look no further! I highly recommend ‘From Code to Connections’, a book that will guide you through the process. Don’t miss out, grab your copy now on Amazon worldwide or Amazon India! You can also go for Gumroad

Converting RGB to grayscale

I usually use Google Colab to write and run the code. You can access the full code in Google Colab here.

We will go through these main steps:

  1. Load the image
  2. Set color channel values
  3. Convert image from lab space to RGB
  4. Plot the output

Before jumping into these steps, first, import the required libraries in your notebook.

import cv2
import numpy as np
from skimage.io import imread
from skimage.color import rgb2lab, lab2rgb
import matplotlib.pylab as plt
from google.colab.patches import cv2_imshow
from google.colab import files

Now load the image you want to test it on.

def read_file(filename):
image = cv2.imread(filename)
cv2_imshow(image)
return image
uploaded = files.upload()filename = next(iter(uploaded))
image = read_file(filename)

Run the cell, you will see the output as two options. Upload the image of your choice. If you are running this project in the Jupyter notebook, then you can simply add the path of the image. You don’t have to do all this.

Convert your image from RGB to Lab Color Space.

image1 = rgb2lab(image)

Set the color values of the second and third channels to zeros.

image1[...,1] = image1[...,2] = 0

Get the grayscale image by converting the image back to RGB from Lab Color Space.

image1 = lab2rgb(image1)

Plot the output of the image.

plt.figure(figsize=(20,10))
plt.subplot(122), plt.imshow(image1), plt.axis('off'), plt.title('Gray Scale Image', size=20)
plt.show()

And that’s it. You are done. I converted one RGB image of my childhood crush to grayscale, using the above program. Have a look.

Awesome!!! I am talking about her.

Now let’s dig more into this. We have set the color value of the second and third channels to zero. Remove one channel from there, set only one channel to zero and re-run this project and see what changes in the output.

Thanks for Reading !!, and if this article is helpful then Clap until your hands bleed.

--

--

Rohit Kumar Thakur
Rohit Kumar Thakur

Written by Rohit Kumar Thakur

I write about AI, Tech, Startup and Code

No responses yet