Here are the ways to convert an image from RGB to Grayscale in Python:
- Using cv2.cvtColor()
- Using image.convert()
- Using Matplotlib
- Using scikit-image
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
# 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
Method 2: Using image.convert()
Follow step by step:
- Import the PIL/Pillow library
- Load the Image using load() method
- 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
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
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
That’s it.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.