[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky]
[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Draw outlined text with PIL in Python

[Text outlined with PIL in Python]

This is another omission from PIL, but it's not one that you need very often so it's pretty excusable. Besides, the workaround is pretty simple.

The following pil_draw_outline_text method draws outlined text on an ImageDraw.Draw object.

def pil_draw_outline_text(dr, x, y, text, fill, outline, font, anchor, align, thickness): # Draw an outline of the text. for dx in range(-thickness, thickness + 1): for dy in range(-thickness, thickness + 1): dr.multiline_text((x + dx, y + dy), text=text, fill=outline, font=font, anchor=anchor, align=align) # Draw the text in the middle. dr.multiline_text((x, y), text=text, fill=fill, font=font, anchor=anchor, align=align)

This code simply draws the text repeatedly using the outline color with its X and Y coordinated shifted slightly. It then draws the text one last time in its "correct" position using the desired fill color. That's it!

Download the example to experiment with it and to see additional details.

© 2024 - 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.