There are a lot of Libraries in Python to write text on an image. But In this tutorial, we will learn about PIL, one of the most accessible libraries for writing on images.
PIL is a free Python, open-source library that can be used to process images. In this tutorial, we will learn how to:
If you are ready, let's get started.
In this tutorial, we need:
Let's set up our project and install the requirements.
Create ImageWriter folder:
mkdir ImageWriter
Enter to ImageWriter:
cd ImageWriter
Create virtual envormment:
python3 -m venv venv
Activate the virtual environment:
source venv/bin/activate
Now our virtual environment is ready to install PIL.
Install PIL via pip:
pip3 install pillow
Now, we need an image to write on it. If you already have the image, that's good. If not, you can download my image on Github.
The image:
Now let's create the images folder and put the image in it:
mkdir images
Create another folder for the destination of the writing images:
mkdir dest
Also, We need to download a font.ttf. Therefore, to download the font first, go to 1001freefonts.com. Then choose your preferred font and finally download it by clicking "DOWNLOAD".
I’ve Downloaded AgentOrange.ttf.
Finally, we need to create a folder to store our font:
mkdir font
After creating the write.py, This is how our project structure looks:
├── ImageWriter
│
├── dest
│
├── font
│ ├── AgentOrange.ttf
│
├── images
│ └── MyImage.png
│
├── venv
│
└── write.py
To write on an image, we need the following modules of PIL:
In the following example, we'll write Hello World on MyImage.png. Open write.py and put the following code.
from PIL import Image, ImageDraw, ImageFont # 👉️ Import modules from PIL
my_text = "Hello World" # 👉️ Text to add to Image
font_path = "font/AgentOrange.ttf" # 👉️ Font .ttf Path
font_size = 100 # 👉️ Font Size
x, y = 50, 30 # 👉️ Top left corner of the text
img = Image.open(f'images/MyImage.png') # 👉️ Open Image
dr = ImageDraw.Draw(img) # 👉️ Create New Image
my_font = ImageFont.truetype(font_path, font_size) # 👉️ Initialize Font
dr.text((x, y), my_text, font=my_font) # 👉️ Add text to image
img.save("dest/new_img.png") # 👉️ save Image
Let me explain the code:
We can control the position of the text by setting the top left corner. In the following example, we will center the text:
from PIL import Image, ImageDraw, ImageFont # 👉️ Import modules from PIL
my_text = "Hello World" # 👉️ Text to add to Image
font_path = "font/AgentOrange.ttf" # 👉️ Font .ttf Path
font_size = 100 # 👉️ Font Size
x, y = 250, 250 # 👉️ top left corner of the text
img = Image.open(f'images/MyImage.png') # 👉️ Open Image
dr = ImageDraw.Draw(img) # 👉️ Create New Image
my_font = ImageFont.truetype(font_path, font_size) # 👉️ Initialize Font
dr.text((x, y), my_text, font=my_font) # 👉️ Add text to image
img.save("dest/img_text_center.png") # 👉️ save Image
We can also paint the text By setting the fill parameter. However, we can use the color name or RGB.
Yellow color:
from PIL import Image, ImageDraw, ImageFont # 👉️ Import modules from PIL
my_text = "Hello World" # 👉️ Text to add to Image
font_path = "font/AgentOrange.ttf" # 👉️ Font .ttf Path
font_size = 100 # 👉️ Font Size
x, y = 250, 250 # 👉️ top left corner of the text
color = "yellow" # 👉️ Color
img = Image.open(f'images/MyImage.png') # 👉️ Open Image
dr = ImageDraw.Draw(img) # 👉️ Create New Image
my_font = ImageFont.truetype(font_path, font_size) # 👉️ Initialize Font
dr.text((x, y), my_text, font=my_font, fill=color) # 👉️ Add text to image
img.save("dest/img_text_color.png") # 👉️ save Image
As you can see, the color of the text is changed to yellow. Now let's use RGB color.
from PIL import Image, ImageDraw, ImageFont # 👉️ Import modules from PIL
my_text = "Hello World" # 👉️ Text to add to Image
font_path = "font/AgentOrange.ttf" # 👉️ Font .ttf Path
font_size = 100 # 👉️ Font Size
x, y = 250, 250 # 👉️ top left corner of the text
rgb_color = (255,0,0,255) # 👉️ Rgb Color
img = Image.open(f'images/MyImage.png') # 👉️ Open Image
dr = ImageDraw.Draw(img) # 👉️ Create New Image
my_font = ImageFont.truetype(font_path, font_size) # 👉️ Initialize Font
dr.text((x, y), my_text, font=my_font, fill=rgb_color) # 👉️ Add text to image
img.save("dest/img_text_color_rgb.png") # 👉️ save Image
Check out RGB Calculator to get the RGB colors.
In the following example, we will set the color red for the "Hello" word and yellow for "World":
from PIL import Image, ImageDraw, ImageFont # 👉️ Import modules from PIL
my_text = "Hello World" # 👉️ Text to add to Image
font_path = "font/AgentOrange.ttf" # 👉️ Font .ttf Path
font_size = 100 # 👉️ Font Size
x, y = 250, 250 # 👉️ top left corner of the text
colors = ["red", "yellow"] # Multi Colors
img = Image.open(f'images/MyImage.png') # 👉️ Open Image
dr = ImageDraw.Draw(img) # 👉️ Create New Image
my_font = ImageFont.truetype(font_path, font_size) # 👉️ Initialize Font
for color, word in zip(colors, my_text.split()): # 👉️ Loop over the colors and text split
dr.text((x, y), word, font=my_font, fill=color) # 👉️ Add text to image
x += x * 2
img.save("dest/img_two_words.png") # 👉️ save Image
In this tutorial, we've learned how to use Python PIL to write text on an image. We've also learned how to style the text.
You can download the project on GitHub.
If you need to learn more about PIL, I recommend going to pillow (PIL Fork) documentation. For more about Python and Django tutorials, visit the Pytutorial blog.