Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert RGB to black OR white

How would I take an RGB image in Python and convert it to black and white? Not grayscale, I want each pixel to be either fully black (0, 0, 0) or fully white (255, 255, 255).

Is there any built-in functionality for getting it done in the popular Python image processing libraries? If not, would the best way be just to loop through each pixel, if it's closer to white set it to white, if it's closer to black set it to black?

like image 920
Tom Avatar asked Sep 13 '13 03:09

Tom


People also ask

How do I convert a RGB image to grayscale?

You just have to take the average of three colors. Since its an RGB image, so it means that you have add r with g with b and then divide it by 3 to get your desired grayscale image. Its done in this way.

How do I convert a color image to grayscale?

Right-click the picture that you want to change, and then click Format Picture on the shortcut menu. Click the Picture tab. Under Image control, in the Color list, click Grayscale or Black and White.

Why do we convert RGB to grayscale?

Because it is a one layer image from 0-255 whereas the RGB have three different layer image. So that is a reason we prefer grey scale image instead of RGB.

What is gray scale RGB?

Gray Among The RGB - You Try It The RGB scale is calibrated so that when a color's three red/green/blue numbers are equal, the color is a shade of gray. E.g. red=50 green=50 blue=50 is gray, without any bias towards red, green, or blue hue.


3 Answers

Scaling to Black and White

Convert to grayscale and then scale to white or black (whichever is closest).

Original:

meow meow tied up cat

Result:

Black and white Cat, Pure

Pure Pillow implementation

Install pillow if you haven't already:

$ pip install pillow

Pillow (or PIL) can help you work with images effectively.

from PIL import Image

col = Image.open("cat-tied-icon.png")
gray = col.convert('L')
bw = gray.point(lambda x: 0 if x<128 else 255, '1')
bw.save("result_bw.png")

Alternatively, you can use Pillow with numpy.

Pillow + Numpy Bitmasks Approach

You'll need to install numpy:

$ pip install numpy

Numpy needs a copy of the array to operate on, but the result is the same.

from PIL import Image
import numpy as np

col = Image.open("cat-tied-icon.png")
gray = col.convert('L')

# Let numpy do the heavy lifting for converting pixels to pure black or white
bw = np.asarray(gray).copy()

# Pixel range is 0...255, 256/2 = 128
bw[bw < 128] = 0    # Black
bw[bw >= 128] = 255 # White

# Now we put it back in Pillow/PIL land
imfile = Image.fromarray(bw)
imfile.save("result_bw.png")

Black and White using Pillow, with dithering

Using pillow you can convert it directly to black and white. It will look like it has shades of grey but your brain is tricking you! (Black and white near each other look like grey)

from PIL import Image 
image_file = Image.open("cat-tied-icon.png") # open colour image
image_file = image_file.convert('1') # convert image to black and white
image_file.save('/tmp/result.png')

Original:

meow meow color cat

Converted:

meow meow black and white cat

Black and White using Pillow, without dithering

from PIL import Image 
image_file = Image.open("cat-tied-icon.png") # open color image
image_file = image_file.convert('1', dither=Image.NONE) # convert image to black and white
image_file.save('/tmp/result.png')
like image 106
Kyle Kelley Avatar answered Oct 11 '22 23:10

Kyle Kelley


I would suggest converting to grayscale, then simply applying a threshold (halfway, or mean or meadian, if you so choose) to it.

from PIL import Image

col = Image.open('myimage.jpg')
gry = col.convert('L')
grarray = np.asarray(gry)
bw = (grarray > grarray.mean())*255
imshow(bw)
like image 5
askewchan Avatar answered Oct 12 '22 00:10

askewchan


img_rgb = cv2.imread('image.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
(threshi, img_bw) = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
like image 3
monti Avatar answered Oct 11 '22 23:10

monti