In this tutorial, we are going to learn how to change the grayscaling of an image using Grayscaling is the process of changing the images from different colour spaces like RGB,CMYK, etc.., to shades of gray. Install the OpenCV module if you didn't install it before.
pip install opencv-python
After installing the OpenCV module. Follow the below steps to write the code.
# importing the opencv(cv2) module import cv2 # reading the image image = cv2.imread('lion.png') # changing the color space gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # showing the resultant image cv2.imshow('Grayscale Lion', gray_image) # waiting until key press cv2.waitKey() # destroy all the windows cv2.destroyAllWindows()
If you run the above code, then you will see the image in grayscale as shown below.
If you have any doubts in the tutorial, mention them in the comment section.