Skip to content
Advertisement

Using PIL to turn a RGB image into a pure black and white image

I’m using the Python Imaging Library for some very simple image manipulation, however I’m having trouble converting a greyscale image to a monochrome (black and white) image. If I save after changing the image to greyscale (convert(‘L’)) then the image renders as you would expect. However, if I convert the image to a monochrome, single-band image it just gives me noise as you can see in the images below. Is there a simple way to take a colour png image to a pure black and white image using PIL / python?

from PIL import Image 
import ImageEnhance
import ImageFilter
from scipy.misc import imsave
image_file = Image.open("convert_image.png") # open colour image
image_file= image_file.convert('L') # convert image to monochrome - this works
image_file= image_file.convert('1') # convert image to black and white
imsave('result_col.png', image_file)

Original Image Converted Image

Advertisement

Answer

from PIL import Image 
image_file = Image.open("convert_image.png") # open colour image
image_file = image_file.convert('1') # convert image to black and white
image_file.save('result.png')

yields

enter image description here

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement