Drawing on Images with Pillow

The ImageDraw module in Pillow provides tools to draw shapes, lines, and text on images. These features are useful for creating annotations, highlighting areas, or generating custom graphics.


Using the ImageDraw Module

Before you start drawing, create a Draw object associated with an image.

from PIL import Image, ImageDraw
 
# Open an image
img = Image.new("RGB", (400, 400), color="white")
 
# Create a Draw object
draw = ImageDraw.Draw(img)

Drawing Shapes

Drawing Lines

Use the line() method to draw lines on an image.

# Draw a red line
start_point = (50, 50)
end_point = (350, 50)
draw.line([start_point, end_point], fill="red", width=3)

Drawing Rectangles

Use the rectangle() method to draw rectangles.

# Draw a blue rectangle
top_left = (50, 100)
bottom_right = (200, 200)
draw.rectangle([top_left, bottom_right], outline="blue", width=2)

Drawing Circles and Ellipses

Use the ellipse() method to draw ellipses or circles.

# Draw a green circle
bounding_box = [250, 100, 350, 200]
draw.ellipse(bounding_box, outline="green", width=3)

Adding Text to Images

The text() method allows you to add custom text to images.

# Add text to the image
text_position = (50, 250)
text = "Hello, Pillow!"
font_color = "black"
draw.text(text_position, text, fill=font_color)

To use custom fonts, import the ImageFont module and specify the font file and size:

from PIL import ImageFont
 
# Load a custom font
font = ImageFont.truetype("arial.ttf", size=20)
draw.text((50, 300), "Custom Font Example", fill="purple", font=font)

Combining Shapes and Text

Create annotated images by combining shapes and text:

# Draw a rectangle and label it
top_left = (100, 100)
bottom_right = (300, 200)
draw.rectangle([top_left, bottom_right], outline="black", width=2)
draw.text((100, 210), "Labeled Rectangle", fill="black")

Try It Yourself

Problem 1: Draw a Target

Write a script to create an image with concentric circles resembling a target.

Show Solution
from PIL import Image, ImageDraw
 
# Create a blank image
img = Image.new("RGB", (400, 400), "white")
draw = ImageDraw.Draw(img)
 
# Draw concentric circles
colors = ["red", "blue", "green", "yellow"]
radius = 150
center = (200, 200)
 
for color in colors:
    draw.ellipse([center[0] - radius, center[1] - radius, center[0] + radius, center[1] + radius], outline=color, width=5)
    radius -= 30
 
img.show()

Problem 2: Add Text with Shapes

Create an image with a rectangle and add a label inside it.

Show Solution
from PIL import Image, ImageDraw, ImageFont
 
# Create a blank image
img = Image.new("RGB", (400, 400), "white")
draw = ImageDraw.Draw(img)
 
# Draw a rectangle
top_left = (100, 150)
bottom_right = (300, 250)
draw.rectangle([top_left, bottom_right], outline="blue", width=3)
 
# Add a label inside the rectangle
font = ImageFont.truetype("arial.ttf", size=20)
draw.text((120, 180), "Labeled Box", fill="blue", font=font)
 
img.show()

With the ImageDraw module, you can create custom graphics, annotate images, and enhance visual presentations. Experiment with these techniques to unlock creative possibilities!