Python PIL – Apply sketch effect

In this post we will be creating a Python script that will apply a sketch effect to an existing image, 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 below where we utilise the PIL library to apply a sketch effect to an image and then save it. We start by opening the image and then converting it to grayscale. Using the ‘ImageFilter’ module we then apply a contour effect to our image.

from PIL import Image, ImageFilter

img = Image.open('image.jpg')

img = img.convert('L')

img_sketch = img.filter(ImageFilter.CONTOUR)

img_sketch.save('sketch_image.jpg')

See an example image below before the effect.

Python PIL - Apply sketch effect before

See an example image below with the effect.

Python PIL - Apply sketch effect after

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply