Convert 3 channel black and white image to Binary



If you have only 2 colors in your image lets say black and white(it can be any other color also) and want to convert it into a binary image, you can convert it using the method shown below. 

A black and white images can be created in 2 ways 

1. Where each pixel in image is represented by 3 values (Red,Green,Blue). In this case the pixels representing white are represented by RGB value (255,255,255)  while the black pixels are represented by RGB values (0,0,0).  This type of image is still called an RGB image because it has 3 channels.

2. Another way to create a black and white image is to create a single channel grayscale image,  where each pixel is represented by only one value between 0 and 255, where 0 is black and 255 is white. Values between 0 to 255 corresponds to intermediate values of black being converted to white (as in the different shades of gray).

If you have an image that falls into category of what is described in 1  (for example see the image below)


then you need to convert this image to Grayscale first in order to convert it to a binary image. To do this using python we will use PIL library.

from PIL import Image, ImageOps
import numpy as np

#open file and convert to single channel Grayscale image
f="test.png"
img = Image.open(f).convert('L')

Once the image is converted into a grayscale image it is easy to convert it into a binary image of 0 and 1. If your grayscale image contains only 2 unique values for example 0 for black and 255 for white then you can flip all 255 valued pixels to 1 and keep 0 as 0. You can also apply any custom conversion logic like for example convert all pixels with value less than 50 to 0 and remaining to 1. Now lets convert the grayscale image into binary.
#invert the grayscale image first
img_inverted = ImageOps.invert(img)

#convert to numpy array once inverted
npimg = np.array(img_inverted)

#convert all values in numpy array to 1 if it is greater than 0
#do nothing to values less than 0
npimg[npimg > 0] = 1
If you want to save the converted binary image back to disk then do this
#convert the numpy array containing 0 and 1's into a PIL image and save
im = Image.fromarray(npimg)
im.save(filename)

When you try to visualize the saved binary image using a image viewer application like Preview(on Mac) or Pictures(on Windows) it will appear entire black.

If you want to verify whether it was converted correctly or not you can check the existence of 0 and 1's by loading the image and counts of 0 and 1 values.
This can be done as shown below 
#open binary image using PIL
im = Image.open('binray_image.png')
#convert the opened image to numpy array
np_arr = np.array(im)
#check how the unique values in the numpy array (it should be 0 and 1) and their respective counts
print(np.unique(np_arr, return_counts=True))

Comments

Popular posts from this blog

Developing Garmin SmartWatch Apps with ConnectIQ Platform-Part 3

Developing Garmin SmartWatch Apps with ConnectIQ Platform-Part 1

Web Service Security and Native Mobile App User Authentication