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.
Steps to convert Color image to black and white image
Steps to convert a given color image to a grayscale image.
- Import Image module from Pillow library.
- Read input image using Image.open() function.
- Convert image to black and white image using Image.convert() function.
- 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]
Black and white image [black_white_image.jpg]
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.