$ cat tips/pillow-bold-text-variable-font.md

Pillow: bold text from a variable font

Pillow has no bold parameter, because the weight of the letters comes from the font file. If the font is variable, one file is enough. After loading it, set the weight axis with set_variation_by_axes([700]). This works on woff2 files too, the same ones the website serves.

from PIL import ImageFont

font = ImageFont.truetype("ibm-plex-sans-latin.woff2", 48)
font.set_variation_by_axes([700])

The list has as many numbers as the font has axes, in the order font.get_variation_axes() returns. Mine has one axis, a weight from 100 to 700.

A name works in place of the number: font.set_variation_by_name("Bold"). font.get_variation_names() returns the names there are.

The call changes the font object, so for two weights in one image I load two objects. A font that is not variable answers the call with OSError: invalid argument.

This is how I draw the images that show in the link preview of each of these tips. The Polish letters come from the latin-ext file and the rest from latin: I cut the text into runs and draw each with the right font.

checked: Pillow 12.3.0, FreeType 2.14.3, Python 3.13

$ ls ./blog/tips
all tips →
↑