Python PIL – Posterize an image

In this post we will be creating a Python script that will posterize an existing image, this will reduce the number of tones present in the image by mapping similar colours to a smaller set of distinct colours. 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 posterize an image and save it as a new file. Below we have applied a posterize filter with 4 levels

from PIL import Image

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

img = img.quantize(colors=4, method=0)

img.save('adjusted_image.png')

See below our image before the adjustment.

Python PIL - Posterize an image BEFORE

See below our image after the adjustment.

Python PIL - Posterize an image AFTER

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

Leave a Reply