How to Draw Text and Shapes on Images with Pillow
In the world of digital image processing, the ability to draw text and shapes on images is a fundamental yet powerful skill. Pillow, a popular Python Imaging Library (PIL) fork, provides a simple and effective way to perform such operations. Whether you’re creating visual content for a website, adding watermarks to photos, or generating data visualizations, Pillow can help you achieve your goals. This blog post will guide you through the process of drawing text and shapes on images using Pillow, covering core concepts, typical usage scenarios, common pitfalls, and best practices.
Table of Contents
- Core Concepts
- Typical Usage Scenarios
- Drawing Text on Images
- Drawing Shapes on Images
- Common Pitfalls and Best Practices
- Conclusion
- References
Core Concepts
Pillow Library
Pillow is a free and open - source Python library for opening, manipulating, and saving many different image file formats. It provides a high - level interface to work with images, making it easy to perform complex operations such as resizing, cropping, and adding text and shapes.
Image and Draw Objects
- Image Object: Represents an image in Pillow. You can open an existing image using the
Image.open()function or create a new one usingImage.new(). - Draw Object: Used to draw text and shapes on an image. You can create a
Drawobject from anImageobject using theImageDraw.Draw()function.
Coordinate System
Pillow uses a Cartesian coordinate system where the origin (0, 0) is at the top - left corner of the image. The x - coordinate increases from left to right, and the y - coordinate increases from top to bottom.
Typical Usage Scenarios
Watermarking
Adding a watermark to an image is a common way to protect your intellectual property. You can draw text (such as your logo or copyright information) on an image using Pillow.
Data Visualization
When creating visualizations, you may need to add labels, titles, or annotations to images. Pillow allows you to draw text and shapes to enhance the clarity of your visualizations.
Image Editing
You can use Pillow to add shapes (such as rectangles, circles, or lines) to an image for artistic or functional purposes, like highlighting a specific area.
Drawing Text on Images
The following is a Python code example using Pillow to draw text on an image:
from PIL import Image, ImageDraw, ImageFont
# Open an existing image
image = Image.open('example.jpg')
# Create a Draw object
draw = ImageDraw.Draw(image)
# Specify the font and size
font = ImageFont.truetype('arial.ttf', 36)
# Specify the text and its position
text = "Hello, World!"
position = (50, 50)
# Draw the text on the image
draw.text(position, text, font=font, fill=(255, 0, 0))
# Save the modified image
image.save('output_text.jpg')
Explanation
- Importing Libraries: We import the necessary classes from the
PILlibrary. - Opening the Image: We use
Image.open()to open an existing image. - Creating a Draw Object: We create a
Drawobject from theImageobject usingImageDraw.Draw(). - Specifying the Font: We use
ImageFont.truetype()to specify the font and its size. - Drawing the Text: We use the
text()method of theDrawobject to draw the text on the image at the specified position. - Saving the Image: We save the modified image using the
save()method.
Drawing Shapes on Images
The following is a Python code example using Pillow to draw shapes on an image:
from PIL import Image, ImageDraw
# Create a new blank image
image = Image.new('RGB', (500, 500), color=(255, 255, 255))
# Create a Draw object
draw = ImageDraw.Draw(image)
# Draw a rectangle
rectangle = [(100, 100), (300, 300)]
draw.rectangle(rectangle, outline=(0, 0, 0))
# Draw a circle
circle = [(200, 200), (400, 400)]
draw.ellipse(circle, outline=(0, 0, 255))
# Draw a line
line = [(100, 400), (400, 400)]
draw.line(line, fill=(0, 255, 0), width=5)
# Save the image
image.save('output_shapes.jpg')
Explanation
- Creating a New Image: We use
Image.new()to create a new blank image with a specified size and background color. - Creating a Draw Object: We create a
Drawobject from theImageobject usingImageDraw.Draw(). - Drawing Shapes:
- Rectangle: We use the
rectangle()method to draw a rectangle with the specified coordinates. - Circle: We use the
ellipse()method to draw a circle (an ellipse with equal width and height) with the specified coordinates. - Line: We use the
line()method to draw a line between two points with a specified color and width.
- Rectangle: We use the
- Saving the Image: We save the modified image using the
save()method.
Common Pitfalls and Best Practices
Common Pitfalls
- Font Availability: If the specified font file (e.g.,
arial.ttf) is not available on the system, aIOErrorwill be raised. You can use system - available fonts or make sure the font file is in the correct path. - Coordinate Calculation: Incorrect coordinate calculation can lead to text or shapes being drawn outside the visible area of the image. Always double - check your coordinates.
- Color Representation: Pillow uses RGB color representation. Incorrect color values can result in unexpected colors.
Best Practices
- Error Handling: Wrap your code in try - except blocks to handle potential errors, such as file not found or font not available.
- Testing on a Small Scale: Before applying changes to a large number of images, test your code on a single image to ensure it works as expected.
- Using Relative Coordinates: When possible, use relative coordinates (e.g., based on the size of the image) to make your code more flexible.
Conclusion
Pillow is a powerful and versatile library for drawing text and shapes on images. By understanding the core concepts, typical usage scenarios, and following best practices, you can effectively use Pillow to enhance your images for various purposes. Whether you’re a beginner or an experienced developer, Pillow provides a simple and intuitive way to work with images in Python.
References
- Pillow Documentation: https://pillow.readthedocs.io/en/stable/
- Python Official Documentation: https://docs.python.org/3/