Create Outline Text in Python with Pillow
Contents
Sometimes, we may want to add surrounding color to text strokes in Python.
Prior to version 6.2.0, we can not do this in Pillow. Fortunately, starting
from version 6.2.0, pillow now supports text outlines natively in the
ImageDraw.text()
method.
from PIL import Image, ImageFont, ImageDraw
def main():
im = Image.new('RGB', (500, 500), (255, 255, 255))
text = "测试文字"
font = ImageFont.truetype(
font="C:/WINDOWS/Fonts/STKAITI.TTF",
size=70)
drawer = ImageDraw.Draw(im)
fill_color = (255, 0, 0)
stroke_color = (0, 0, 255)
drawer.text((50, 10), text, font=font, fill=fill_color, stroke_width=1, stroke_fill=stroke_color)
drawer.text((50, 80), text, font=font, fill=fill_color, stroke_width=3, stroke_fill=stroke_color)
drawer.text((50, 150), text, font=font, fill=fill_color, stroke_width=5, stroke_fill=stroke_color)
drawer.text((50, 220), text, font=font, fill=fill_color, stroke_width=7, stroke_fill=stroke_color)
im.show("text-image")
if __name__ == "__main__":
main()
The parameter stroke_width
controls the width of outline, and stroke_fill
controls the outline color.
The above script will create the following image.
Ref: