Color image to Gray scale image in Python

In this tutorial, we will explore how to alter the gray tones of an image using grayscaling. Grayscaling involves transforming images from various color formats such as RGB and CMYK to different shades of gray. Make sure to install the OpenCV module if it is not already installed on your system to follow along with the tutorial.

To convert a color image to a grayscale image in OpenCV, you have two options. The first method is to convert the image to grayscale by using the imread() function. Another way is to change the color image to grayscale using the cvtColor() function. Both of these functions allow you to easily transform a color image into a grayscale one in OpenCV.

Method 1: Using imread() function

Inorder to convert the Color image to Gray scale image the imread() function in OpenCV is used for reading images, but there is an additional parameter to consider called a flag, which determines how the image is read. In OpenCV, there are three flags defined that you can use when reading an image. These flags help specify the way the image should be processed and displayed within your program. It is important to understand and choose the appropriate flag according to your specific image processing needs when using the imread() function in OpenCV.

cv2.IMREAD_COLOR or 1

cv2.IMREAD_GRAYSCALE or 0

cv2.IMREAD_UNCHANGED or -1

To convert a color image to grayscale, you can use the function cv2.imread(“image-name.png”, 0) or simply write cv2.IMREAD_GRAYSCALE instead of 0 since it represents the same constant. This process helps in converting the image from color to shades of gray. Grayscale images are in black and white, making them easier to analyze and process. By applying this method, you can effectively change the visual representation of the image.

import cv2

# Reading color image as grayscale
gray = cv2.imread("color-img.png",0)

# Showing grayscale image
cv2.imshow("Grayscale Image", gray)

# waiting for key event
cv2.waitKey(0)

# destroying all windows
cv2.destroyAllWindows()

Method 2: Using cvtColor() function

The cvtColor() function in OpenCV is a useful tool for changing color channels from one type to another, like BGR to HSV or BGR to RGB. It can also convert BGR to GRAY by utilizing cv2.cvtColor(img, cv2.BGR2GRAY). This function helps in manipulating images to ensure they are in the desired color format for further processing. By understanding how cvtColor() works, you can enhance your image processing capabilities significantly.

​import cv2

# Reading color image
img = cv2.imread("color-img.png")

# Converting color image to grayscale image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Showing the converted image
cv2.imshow("Converted Image",gray)

# waiting for key event
cv2.waitKey(0)

# destroying all windows
cv2.destroyAllWindows()
Example

 

IMG

Conclusion

In conclusion, we learned two ways to change a color picture into a black-and-white picture. We discovered the functions cvtColor() and imread() flags in OpenCV that help us make this transformation. These tools are valuable for converting images to grayscale for various purposes. With the knowledge gained, we can now easily create grayscale images using OpenCV functions.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top