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.

RGB image converted to grayscale image

Steps to convert Color image to Grayscale image

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

  1. Import Image, and ImageFilter modules from Pillow library.
  2. Read input image using Image.open() function.
  3. Convert image to grayscale image using Image.convert() function.
  4. 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]

Pillow example to Convert image to grayscale - input image

Grayscale image [grayscale_image.jpg]

Pillow example to Convert image to grayscale - resulting image

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.