There are a lot of Libraries in Python to write text on an image. But In this tutorial, we will learn about , one of the most accessible libraries for writing on images. PIL 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 to write on it. If you already have the image, that's good. If not, you can download my image on . image 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 . Therefore, to download the font first, go to . Then choose your preferred font and finally download it by clicking " ". font.ttf 1001freefonts.com DOWNLOAD I’ve Downloaded AgentOrange.ttf. Finally, we need to create a folder to store our font: mkdir font Project structure After creating the , This is how our project structure looks: write.py ├── 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: : to open the given image. Image : to create a new image from the given image. ImageDraw : to Initialize the font. ImageFont In the following example, we'll write on Open and put the following code. Hello World MyImage.png. write.py 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: Importing Image, ImageDraw and ImageFont modules. Declaring my_text with the Hello World Value. Declaring font_path and font_size. Declaring the top left corner of the text. Opening MyImage.png . Generating a new image. Initializing the font. Adding to the image. my_text Saving the new image in the folder. dest We can control the position of the text by setting the . In the following example, we will the text: top left corner center 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 parameter. However, we can use the color name or RGB. fill color: Yellow 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 . Now let's use RGB color. yellow 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 to get the RGB colors. RGB Calculator In the following example, we will set the color for the "Hello" word and for "World": red yellow 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 to write text on an image. We've also learned how to style the text. PIL You can download the project on . GitHub If you need to learn more about PIL, I recommend going to . For more about Python and Django tutorials, visit the . pillow (PIL Fork) documentation Pytutorial blog