How to add text on an image using Pillow in Python?

Pillow provides several tools and functions for working with images, including the ability to add text to an image. Adding text to an image can be useful for creating captions, labels, or annotations for images. In this article, we will explore how to add text on an image using Pillow in Python. We will discuss different methds for adding text, such as changing the font, size, color, and position of the text. We will also provide step-by-step instructions and code examples to help you get started with adding text on an image using Pillow.


Python Pillow module is constructed on the top of PIL (Python Image Library). It is the core Python module for image processing. The pillow is a PIL derivative created by Jeffrey A. Clark (Alex) and other contributors. PIL is the Python Imaging Library developed by Frederik Lundh and others. But Python 3 does not support it. However, this module can be used with Python 3.x versions as PIL. It supports various image formats, including jpeg, png, bmp, gif, ppm, and tiff.

You can do more with images in Python with the help of the Python Imaging Library. This library offers support for a wide variety of file formats, has an effective internal representation, and is capable of performing pretty complex image processing operations. The core image library was developed to provide quick access to the data that is contained in a small number of fundamental pixel formats. It ought to provide a solid and sturdy basis for the development of a general image processing tool.

Add text on an image using Pillow

Python's PIL (Python Imaging Library) is a separate library used for opening images, manipulating images, and storing images in a variety of formats. Using this PIL, we may do a wide variety of actions on photos, like creating new Images, modifying current Images, rotating existing Images, etc. We will use the PIL library to add the text on an image.


Image_object.text((a, b) , Text, desired_font , fill )


In the above syntax,

  • The a and b indicate the beginning position/coordinate (in pixels) for adding text to an image.

  • The Text is the string or text we want to add to our image.

  • Desired_font is the font size or font type that we want to give to our sample text.

  • Fill is used to provide color to our text.

We will see step by step procedure to add the text on an image using the Pillow library of Python.

Step 1: Import the Python Library Pillow

Let's begin by installing the library that will be required for this undertaking. After the installation is complete, the library can be imported for use in the project. Using PIP, a Python package manager utility, is the easiest method for installing this library. Compatible with the vast majority of Python libraries.

pip install pillow

We can import all the important packages in just one line −

from PIL import Image, ImageFont, ImageDraw

Step 2: Choose a particular image on which you want to add text

This step involves selecting and importing an image to which text will be added. We will define a new variable(i) and will assign that particular image to the variable using the method open(). Below is the syntax of the open() method −“sample.png”)

Step 3: Define a text and add the text to the image

In the previous step, we already chose the image. Firstly, we will define a variable for the text and we will start rendering the image.

We will create an object for the image using the ImageDraw module.

Im = ImageDraw.Draw(i)

There are three parameters that we will pass to the text() function.

Im.text((60, 50), "Marilyn Monroe",fill=(0, 0, 0))
  • Starting Coordinates: The Cartesian pixel coordinate system is used by the Pillow library, with (0,0) in the upper left quadrant.

  • A string enclosed in single or double quotation marks

  • Text color formatted in RGB: Google Picker is an excellent tool for locating the ideal color. Google "Color Selector" will return results for this tool.

Step 4: Save the image

The last step is to save the image using the save() function."mm.png")

Example 1

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
# Open the desired Image you want to add text on
i ='sample2.jpg')

# To add 2D graphics in an image call draw Method

Im = ImageDraw.Draw(i)
mf = ImageFont.truetype('font.ttf', 25)
# Add Text to an image
Im.text((15,15), "Lady watching movie with her dog", (255,0,0), font=mf)

# Display edited image on which we have added the text

# Save the image on which we have added the text"mm.png")


Example 2

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
# Open the desired Image you want to add text on
i ='sample.jpg')

# Call draw Method to add 2D graphics in an image
Im = ImageDraw.Draw(i)
mf = ImageFont.truetype('font.ttf', 25)
# Add Text to an image
Im.text((90, 10), "Working",fill=(0, 0, 0),font=mf)

# Display edited image

# Save the edited image"sn.png")



We learned how to add text to images in this Python article. First, we used the easiest example to show how we can add text to an image. After that, we talked more about how to use the Python programming language to make changes to the text, such as changing its size, color, and position.

Updated on: 31-May-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started