I\'m using python/PIL to write text on a set of PNG images. I was able to get the font I wanted, but I\'d like to now outline the text in black.
I was looking for that on the internet and I saw that PIL doesn't have native support to add text border. In that case, I create this proposal method based on Alec Bennett solution.
The problem that I found with that solution is about how to create a smooth border on larger border size. Problem Example
If we walk over 360 degrees or 2pi radians, adding the same text, we could create a better implementation of the text border functionality.
Here's the example function
def add_text(image, text, location, font, fontsize=14, fontcolor=(0, 0, 0),
border=0, border_color=(0, 0, 0), points=15):
font_format = ImageFont.truetype(font, fontsize)
drawer = ImageDraw.Draw(image)
if border:
(x, y) = location
for step in range(0, math.floor(border * points), 1):
angle = step * 2 * math.pi / math.floor(border * points)
drawer.text((x - border * math.cos(angle), y - border * math.sin(angle)), text, border_color, font=font_format)
drawer.text(location, text, fontcolor, font=font_format)
return image
The same image, text and config generate the next Final Result