How to Add Text on Image using Python | Add Text on Image using PIL — python
Step 1 — Import Pillow Library
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
Step 2 — Choose an Image
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")
Step 3 — Font Selection
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.
Step 4 — Render 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.
Step 5 — Export the Result
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")