How to Convert RGB Image to Grayscale in Python

Here are the ways to convert an image from RGB to Grayscale in Python:

  1. Using cv2.cvtColor()
  2. Using image.convert()
  3. Using Matplotlib
  4. Using scikit-image

Python RGB Image to Grayscale

Method 1: Using cv2.cvtColor()

The imread() method in OpenCV loads an image from the specified file path. Subsequently, the cvtColor() function converts this image to grayscale.

Install the open-cv library if you have not installed using the below command.

python3 -m pip install opencv-python

# OR

pip install opencv-python

Example

Krunal_10

# Import the OpenCV library
import cv2

# 'Krunal_10.png' is the file name. Replace it with your image file's name.
# This method loads the image in BGR (Blue, Green, Red) color format.
input_image = cv2.imread('Krunal_10.png')

# Convert the loaded image to grayscale
gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)

# Save the grayscale image to a file
cv2.imwrite('gray_image.jpg', gray_image)

# Display the grayscale image in a window
cv2.imshow('Grayscale Image', gray_image)

# Wait for a key press to close the window
cv2.waitKey(0)

# Close all OpenCV windows
cv2.destroyAllWindows()

Output

Output-of-grayscale-image

Method 2: Using image.convert()

Follow step by step:

  1. Import the PIL/Pillow library
  2. Load the Image using load() method
  3. Apply the convert(‘L’) method to the loaded image

Example

from PIL import Image

# Load the image
input_image = Image.open('Krunal_10.png')

# Convert to grayscale
gray_image = input_image.convert('L')

# Save or display the grayscale image
gray_image.save('gray_image.jpg')
gray_image.show()

Output

Output of image.convert()

Method 3: Using Matplotlib

Matplotlib is primarily used for plotting, but it can also be used for basic image operations.

To use this method, you need to install the Matplotlib package:

python3 -m pip install matplotlib

Example

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

# Load the image
input_image = mpimg.imread('Krunal_10.png')

# Weights for the RGB channels to convert to grayscale
# These weights are commonly used in the image processing community
r, g, b = 0.2989, 0.5870, 0.1140
gray_image = r * image[:, :, 0] + g * image[:, :, 1] + b * image[:, :, 2]

# Display the grayscale image
plt.imshow(gray_image, cmap='gray')
plt.axis('off')
plt.show()

Output

Output_of_Using Matplotlib

Method 4: Using scikit-image

The scikit-image is a collection of algorithms for image processing.

You need to install the scikit-image package if you haven’t:

pip install scikit-image

Example

from skimage import io, color
import numpy as np

# Load the image
input_image = io.imread('Krunal_10.png')

# If the image has an alpha channel, remove it
if input_image.shape[-1] == 4:
 image = input_image[:, :, :3]

# Convert to grayscale
gray_image = color.rgb2gray(image)

# Convert the grayscale image to 8-bit (range 0-255)
gray_image_8bit = (gray_image * 255).astype(np.uint8)

# Save or display the grayscale image
io.imsave('gray_image.jpg', gray_image_8bit)
io.imshow(gray_image_8bit)
io.show()

Output

Output_of_Using scikit-image

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.