Title: Draw outlined text 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.
|