Using Python, we can do many cool things and one of those is to play with images. Do you know, you can convert any colorful image to a grayscale one, which will have a nice black and white shade?
Let’s try this out.
Required Packages
To generate a grayscale or black & white image from a given image, we need a package named Open CV that can be installed using pip as shown.
pip install opencv-python
Input Image
To perform this activity, I’m taking a colorful image as input.
Source Code
Here is the source code to generate the grayscale of the above image:
import cv2
image = cv2.imread(‘myImage.png’,0)
cv2.imshow(‘New image’,image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output
On executing the above code, you will get the below output depicting an image in black and white shade.
If you would like to take a code walkthrough explaining which line of code is written for what purpose, I would recommend you to watch my video here.
Happy conversion!