Convert An Image To Grayscale Using Python

Shweta Lodha
2 min readSep 13, 2022

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 and 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 an input:

image source: The Advik Show

Source Code

Here is the source code to generate the grayscale of the above image:

import cv2image = 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:

--

--

Shweta Lodha
Shweta Lodha

Responses (1)