ItSolutionStuff.com

Add Text on Image using Python PIL Library Example

By Hardik Savani • October 30, 2023
Python

Hello Dev,

This example is focused on python add text to image using pil. I would like to share with you python write text on image pil. if you want to see an example of python pil add text to image then you are in the right place. This example will help you how to add text on image in python pil library. Alright, let us dive into the details.

Sometimes, we need to write text on an image and save it or share it with the customer, then I will give you a simple example of adding text on an image with python. we will use PIL library to add text on image. i will share with you two simple examples, one will simply add text on the image, and in another example, we will add text with a custom font family. so let's see both examples one by one.

Example 1:

You need to take the "sample.png" image and put it in your root folder.

main.py

# Importing the PIL library
from PIL import Image
from PIL import ImageDraw
   
# Open an Image from Path
image = Image.open('sample.png')
   
# Call draw Method to add 2D graphics
I1 = ImageDraw.Draw(image)
   
# Add Text to Image
I1.text((500, 400), "This is python post example", fill=(255, 0, 0))
   
# Display edited image
image.show()
   
# Save the image
image.save("sample2.png")

Output:

Example 2:

You need to take the "sample.png" image and put it in your root folder. Then you need to download "arial.ttf" font family and put it into the root folder.

main.py

# Importing the PIL library
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
   
# Open an Image from Path
image = Image.open('sample.png')
   
# Call draw Method to add 2D graphics
I1 = ImageDraw.Draw(image)
  
# Custom font family
myFont = ImageFont.truetype('arial.ttf', 50)
   
# Add Text to Image
I1.text((300, 350), "This is python post example", font=myFont, fill=(255, 0, 0))
   
# Display edited image
image.show()
   
# Save the image
image.save("sample3.png")

Output:

I hope it can help you...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Python JPEG Image to Base64 String Example

Read Now →

Python PNG Image to Base64 String Example

Read Now →

How to Convert Image to Base64 String in Python?

Read Now →

Python Convert base64 String to JPEG Image Example

Read Now →

Python Convert base64 String to PNG Image Example

Read Now →

How to Convert base64 to Image in Python?

Read Now →

How to Pretty Print JSON in Python?

Read Now →

How to Create Text File in Python?

Read Now →

Python List Print All Elements Except Last Example

Read Now →

How to Remove Duplicate Values from List in Python?

Read Now →

Python Remove Empty String from List Example

Read Now →

Python Post Request with Bearer Token Example

Read Now →

Python Subtract Seconds from DateTime Example

Read Now →