Gray scale images

Raghunath D
2 min readJan 29, 2019

In this post, we will learn how to create gray-scale images

A gray-scale (or gray level) image is simply one in which the only colors are shades of gray. The reason for differentiating such images from any other sort of color image is that less information needs to be provided for each pixel.

In a gray-scale image, each pixel has a value between 0 and 255, where zero corresponds to “black” and 255 corresponds to “white”. The values in between 0 and 255 are varying shades of gray, where values closer to 0 are darker and values closer to 255 are lighter.

Code

import cv2image = cv2.imread('bgrflag.jpg')
print(image.shape) # (400, 600, 3)
cv2.imshow("Image", image)
cv2.waitKey()
# option 1 - creating gray scale by converting color imagegray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
print(gray_image.shape) # (400, 600)
cv2.imshow("Grayscale Image", gray_image)
cv2.waitKey()
# option - reading an image in gray scale formatgrayimage = cv2.imread('bgrflag.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow("GrayImage", grayimage)
print(grayimage.shape) # (400, 600)
cv2.waitKey()
cv2.destroyAllWindows()
(left) Color Image ; (right) Gray scale image

Gray-scale images are very common, in part because much of today’s display and image capture hardware can only support 8-bit images.

In addition, gray-scale images are entirely sufficient for many tasks and so there is no need to use more complicated and harder-to-process color images.

--

--

Raghunath D

Software Engineer working in Oracle. Data Enthusiast interested in Computer Vision and wanna be a Machine learning engineer.