Convert image to grayscale in Pillow
Pillow - Convert image to grayscale
To convert given image to grayscale using Pillow library, you can use Image.convert() function.
Steps to convert Color image to Grayscale image
Steps to convert a given color image to a grayscale image.
- Import Image, and ImageFilter modules from Pillow library.
- Read input image using Image.open() function.
- Convert image to grayscale image using Image.convert() function.
- Save the grayscale image using Image.save() function.
Examples
1. Convert image to grayscale
In the following example, we read an image test_image.jpg
, convert this image to grayscale, and save the resulting image as grayscale_image.jpg
.
Pass the argument "L"
to Image.convert() function to convert the given image to grayscale 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.jpg]
Grayscale image [grayscale_image.jpg]
Summary
In this Python Pillow Tutorial, we learned how to convert given color image to a grayscale image using PIL.Image.convert() function, with the help of examples.