How to Add and Edit Text on Images with Python Pillow
This tutorial walks through using Python's Pillow library to load an image, overlay or replace text with custom fonts and colors, and save or display the edited result, providing step‑by‑step code and practical tips for image manipulation.
In this article we explore how to use the Python programming language and the Pillow (PIL) library to edit and beautify images, focusing on adding or modifying text. The technique is useful for image processing, advertising design, and social media content creation.
Pillow (PIL) Library Overview
Pillow is an active fork of the Python Imaging Library (PIL) that offers powerful image processing capabilities. It handles basic loading and displaying as well as complex transformations, and its simple API makes it one of the most popular image libraries in Python.
Practical Example: Adding or Modifying Text on an Image
The following code demonstrates the complete workflow:
from PIL import Image, ImageDraw, ImageFont
import matplotlib.pyplot as plt
# Load the image
img_path = 'C:\Users\heish\Downloads\111a7dd0d5e04bfb9c07dc1f6e0686fa.png'
image = Image.open(img_path)
# Initialize drawing context
draw = ImageDraw.Draw(image)
# Text settings
text = "99.999"
position = (580, 230) # where the new text should appear
text_color = (255, 255, 255)
font_size = 50
font_path = "C:\Windows\Fonts\simsunb.ttf"
font = ImageFont.truetype(font_path, font_size)
# Overwrite the existing number with a rectangle matching the background
rectangle_position = (position[0], position[1], position[0] + 210, position[1] + 60)
draw.rectangle(rectangle_position, fill=(18, 18, 18))
# Draw the new text onto the image
draw.text(position, text, fill=text_color, font=font)
# Save the edited image
edited_img_path = 'C:\Users\heish\Downloads\111a7dd0d5e04bfb9c07dc1f6e0686fa_1.png'
image.save(edited_img_path)
# Display the edited image
plt.imshow(image)
plt.axis('off')
plt.show()
edited_img_pathStep‑by‑Step Technical Points
Load the image : Use Image.open to read the target file.
Initialize drawing context : Create a ImageDraw.Draw object to enable drawing operations on the image.
Text settings : Define the text content, position, color, font size, and font file; these choices greatly affect visual quality.
Background handling : If overwriting existing text, draw a rectangle with the background color to mask the old content before adding new text.
Draw text : Call draw.text with the prepared settings to render the new text.
Save the edited image : Use image.save to write the result to a file.
Display the image : Use matplotlib.pyplot to show the edited image directly in a notebook.
Practical Tips and Considerations
Choose font type and size that harmonize with the image’s visual style.
When covering existing elements, match the rectangle color to the background for seamless replacement.
Combine Pillow with other libraries such as Matplotlib to extend functionality and visualization options.
Conclusion
By following this guide, readers learn how to add or modify text on images using Pillow, a skill applicable across many domains. Pillow’s ease of use makes Python a powerful tool for image editing and processing.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development & AI Practice
DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
