In this post we will be creating a Python script that will covert an existing image to grayscale, to do this we will be using the Python Imaging Library also known as PIL enabling us to image processing capabilities.
See the sample of Python code where we utilise the PIL library to convert to grayscale and save a new copy of the image. To help us achieve this we utilize the ‘Image.convert()’ method, here we set the mode to ‘L’ which uses a single channel image i.e. 256 variations of gray with black and white included.
from PIL import Image
img = Image.open('image.png')
grayscale_img = img.convert('L')
grayscale_img.save('grayscale_image.png')
See below our sample image in its original colour.

See below our grayscale image once the script has been executed.

Take a look at some of our other content around the Python programming language by clicking here.
One thought on “Python PIL – Convert image to grayscale”