To convert an image to grayscale using Python Imaging Library (PIL), you first need to import the Image module from PIL. Then, open the image file by calling the Image.open() function and passing the path of your image as a string argument. Once the image is opened, use the convert() method on the image object with ‘L’ as the parameter which stands for luminance or lightness in an image. This will return a new image that is converted to grayscale. Finally, save the new image using the save() method on the image object.
Here’s a simple code example:
from PIL import Image
img = Image.open('path_to_your_image.jpg')
gray_img = img.convert('L')
gray_img.save('new_gray_image.jpg')