how to convert an image to black and white in python

The colored or RGB images are composed of three color channels which are red, green and blue. Each pixel of an RGB image is composed of varying proportions of red, green, and blue colors which are added together to produce any color in the range of a visible spectrum. In image processing, processing of these multi-channel RGB images is a computationally expensive. Therefore, they are transformed to grayscale images to reduce the computational complexity and processing time. In this tutorial, we will learn how to Convert an image to in Python. These black and white images are also known as grayscale images.

Python provides different modules for image conversion. Some of the common ways are discussed here. If you want to learn more about Python Programming, visit Python Programming Tutorials.

USING OPENCV TO CONVERT AN image INTO Black AND WHITE

A simple binary thresholding technique in OpenCV can be used to convert an image to black and white. We establish a threshold value and each pixel value is compared with this threshold. If the pixel value is less than this threshold, it is set to 0. The pixel value is set to 1 if the pixel value is greater than this threshold. Here, 0 represent black color and 1 represent white color.

To apply thresholding, first of all, we need to convert a colored image to grayscale. Read the image by providing path of the image in imread(“path of image”) command. Then, convert this image to grayscale using cv2.cvtColor(image, color_space_conversion) function. Here, the first parameter specifies the input to be transformed. The opencv read the image in BGR mode which is same as RGB mode. The second parameter i.e., color_space_conversion specifies the color space from which you want to transform and the color pace in which you want to transform. In this case, we are converting BGR mode to grayscale that’s why we have used cv2.COLOR_BGR2GRAY. The next step is the conversion of this grayscale image to black and white image. For this, we’ll apply thresholding as discussed above. The syntax of a binary thresholding function is:

cv2.threshold (image, threshold, max_value, cv2.THRESH_BINARY)

here, image is the grayscale image which you want to transform. The second parameter specifies the threshold value with which all pixel values are compared. Then, the third input parameter is set. If the pixel value is greater then the threshold value, it is changed to the value of parameter “max-value”. In binarization, we want the maximum value to be 255 as the pixel value for black color is 255.

import cv2
from google.colab.patches import cv2_imshow

#read the image 
image = cv2.imread('C:/Users/abc/Desktop/penguins.jpg')

#Convert an image from BGR to grayscale mode 
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  
#Convert a grayscale image to black and white using binary thresholding 
(thresh, BnW_image) = cv2.threshold(gray_image, 125, 255, cv2.THRESH_BINARY)

#display all the images
cv2_imshow(image)
cv2_imshow(gray_image)
cv2_imshow(BnW_image)

From the output, it can be seen that in the grayscale images, each pixel value lie in a range of 0 to 255 represented different shades of gray. On other hand, black and white images consists of either 0 (white) or 1 (Black) pixels.re

Converting image from White to Black with Pillow library

Python pillow library provides a .convert() function to convert the images into grayscale or black and white mode. The syntax of this function is:

image.convert(‘mode’)

Here, image corresponds to the input image which is to be transformed. If you want to transform an image to grayscale, then insert ‘L’ in the mode. This mode has only one channel whose value lies in a range of 0 to 255. It represents black, white and all the shades of gray. The L mode transforms the image to grayscale mode. Use ‘1’ in mode to transform the image into black and white mode. To study in detail about pillow library or .convert() method, visit this link. Now, lets move towards the code.

from PIL import Image

#read the image from path
image = Image.open('C:/Users/abc/Desktop/penguins.jpg')

#Convert it into the grayscale image
grayscale = image.convert('L')

#Converting the same image to black and white mode
BW= image.convert('1')

#save both the images
grayscale.save("grayscale_image.jpg")
BW.save("BW_image.jpg")

Execute the above code and then open the saved files. The output is shown below.

The above code produced black and white image with dithering which is basically noise. The convert() function has an argument dithering. To disable dithering, it is set to NONE.

from PIL import Image

#read the image
color_image = Image.open('C:/Users/abc/Desktop/penguins.jpg')

#convert the image to black and white mode with dither set to None
bw = color_image.convert('1', dither=Image.NONE)

#save the image with name "BW_image.jpg"
bw.save('BW_image.jpg')
black and white image in python

The images are converted to either grayscale or black and white because grayscale images are easier to process as compared to the colored images. It plays a vital role in image processing applications. This topic discusses different methods to convert an image to grayscale or black and white mode. If you have any questions, please let us know. Your feedback matters a lot for us.

Leave a Comment

Your email address will not be published.