Python PIL – Add text to image

In this post we will be creating a Python script that will add text 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 where we utilise the PIL library to add text to an image and save it as a new file. To help us achieve this we utilize the ‘draw.text()’ method which adds a given string as text to the image. The anchor coordinates of the text are also provided, in this example they are 0,0 although this can be changed.

from PIL import Image
from PIL import ImageDraw, ImageFont

img ='image.png')

draw = ImageDraw.Draw(img)
font = ImageFont.truetype('arial.ttf', 32)
draw.text((1, 0), 'Hello, World!', font=font, fill='white')'text_image.png')

See an example image below with added text.

Python PIL - Add text to image

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

Leave a Reply