visit
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.
mkdir ImageWriter
cd ImageWriter
python3 -m venv venv
source venv/bin/activate
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 .
Now let's create the images folder and put the image in it:
mkdir images
mkdir dest
Also, We need to download a font.ttf. Therefore, to download the font first, go to . Then choose your preferred font and finally download it by clicking "DOWNLOAD".
I’ve Downloaded AgentOrange.ttf.
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
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
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
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.
If you need to learn more about PIL, I recommend going to. For more about Python and Django tutorials, visit the .