paint-brush
How to Write on an Image in Pythonby@propy
1,685 reads
1,685 reads

How to Write on an Image in Python

by ProPySeptember 29th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this Python tutorial, we will learn how to Write a text on an image, Change the size of the text and Centre the text on an image

Company Mentioned

Mention Thumbnail
featured image - How to Write on an Image in Python
ProPy HackerNoon profile picture

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:


  • Write a text on an image
  • Change the size of the text
  • Centre the text on an image
  • Change text color


If you are ready, let's get started.


Dependencies

In this tutorial, we need:


  • Python 3
  • PIL


Set up and installation

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


Project structure

After creating the write.py, This is how our project structure looks:


├── ImageWriter
    │
    ├── dest
    │   
    ├── font
    │   ├── AgentOrange.ttf
    │   
    ├── images
    │   └── MyImage.png
    │ 
    ├── venv
    │ 
    └── write.py


Write on image

To write on an image, we need the following modules of PIL:

  • Image: to open the given image.
  • ImageDraw: to create a new image from the given image.
  • ImageFont: to Initialize the font.


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:

  1. Importing Image, ImageDraw and ImageFont modules.
  2. Declaring my_text with the Hello World Value.
  3. Declaring font_path and font_size.
  4. Declaring the top left corner of the text.
  5. Opening MyImage.png .
  6. Generating a new image.
  7. Initializing the font.
  8. Adding my_text to the image.
  9. Saving the new image in the dest folder.


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


Conclusion

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.