Adding Text on Image using Python | by Behic Guven | Towards Data Science

In this post, I will show you how to add text to your images using Python. This will a very simple project where we will use programming to do some design. After this post, you will be able to design your next flyer or business card using some python skills. Doesn’t that sound cool? Maybe not the best way to do the design but it’s possible :)
As a person who likes design and programming, I thought this will be a great project where I can combine my both interests. This is actually a great part of programming, you can always find different areas to practice your skills. In my previous post I showed how to add text to your videos, and today we will do it on an image. Let’s begin!
Import Pillow Library
Choose an Image
Font Selection
Render the Text
Export the Result
Video Demonstration
towardsdatascience.com
First things first, let’s install the library that we will need for this project. After the installation is completed, we can import the library to use it in the project. The best way to install this library is using PIP, which is a python package manager tool. Works perfectly with most of the Python libraries.
pip install pillow
Great, now we can import it to our code. When working with big libraries, instead of importing the whole library, it’s better to practice to import the specific functions that will be used. This will save storage and time when running the program. For our simple project, we will just need three functions: Image, ImageFont, and ImageDraw.
We can import all three of them in one line of code as follows:
from PIL import Image, ImageFont, ImageDraw 
In this step, we will choose and import an image that we want to add text on it. I recommend using Unsplash, which is a great stock photo website to find a good quality image. Here is the image I’ve downloaded that also matches the Fall season:
After downloading the image, make sure to copy it inside the same directory that your code is. This will help you to import it to the program. Let’s define a new variable and assign the image using open method.
my_image = Image.open("nature.jpg")
The good thing about this project, you can choose your font style. Customizing the font will give us more flexibility when designing.
First, we will download the TTF(TrueType Font) file of the font we want to choose. After having the file in the same directory, we can import it to our program using ImageFont function. Here is the font I will use.
title_font = ImageFont.truetype('playfair/playfair-font.ttf', 200)
Now, we can move to the next step, where we will add the text.
This step is where the magic happens. After choosing the image and font, it’s time to decide what to write. Firstly, we will define a text variable and assign a string to it.
title_text = "The Beauty of Nature"
Secondly, we will use the ImageDraw function to convert our image into an editable format. Thanks to the Pillow library, we can do it in one line.
image_editable = ImageDraw.Draw(my_image)
Thirdly, we will do the rendering. There are four parameters we will pass into the rendering function. I will share the descriptions of each parameter below the code with some helpful sources.
image_editable.text((15,15), title_text, (237, 230, 211), font=title_font)
Starting Coordinates: Pillow library uses a Cartesian pixel coordinate system, with (0,0) in the upper left corner.
Text: String between single or double quotations
Text color in RGB format: Google Picker is a great resource to find the best color. Search “Color Picker” on Google and it will show up.
Font style: Google Fonts is a great resource to pick your font style, and you can also download the TTF(TrueType Font) file of the font family.
towardsdatascience.com
Well done! We are almost done. This will be the shortest step where will just export the edited image. Here is the code to export using the save method.
my_image.save("result.jpg")
lifexplorer.medium.com
Congrats!! You have created a program that renders custom text on an image using Python. Not the best way to design an image, but the possibility of doing it using programming is cool. Hoping that you enjoyed reading this article and working on the project. I would be glad if you learned something new today. Working on hands-on programming projects like this one is the best way to sharpen your coding skills.
Feel free to contact me if you have any questions while implementing the code.
Follow my blog and youtube channel to stay inspired. Thank you,