Convert image to black and white in Pillow

Pillow – Convert image to black and white

To convert given image to black and white using Pillow library, you can use Image.convert() function.

Pillow - Convert image to black and white

Steps to convert Color image to black and white image

Steps to convert a given color image to a grayscale image.

  1. Import Image module from Pillow library.
  2. Read input image using Image.open() function.
  3. Convert image to black and white image using Image.convert() function.
  4. Save the black and white image using Image.save() function.

Examples

1. Convert image to grayscale

In the following example, we read an image test_image_house.jpg, convert this image to black and white image, and save the resulting image as black_white_image.jpg.

Pass the argument "1" to Image.convert() function to convert the given image to a black and white image.

Python Program

from PIL import Image

# Open the image
image = Image.open("test_image.jpg")

# Convert the image to grayscale
image = image.convert("L")

# Save the grayscale image
image.save("grayscale_image.jpg")

Original Image [test_image_house.jpg]

Pillow example to Convert image to black and white - input image

Black and white image [black_white_image.jpg]

Pillow example to Convert image to black and white - output image

Summary

In this Python Pillow Tutorial, we learned how to convert given color image to a black and white image using PIL.Image.convert() function, with the help of examples.