Search code examples
pythonfontspython-imaging-library

Choosing a PIL.ImageFont by font name rather than filename, and cross-platform font


ImageFont.truetype requires a filename to work, such as:

font = ImageFont.truetype("ariblk.ttf")  # Arial Black

Is there a way with PIL, to load a font by name, rather than filename?

Context: I would like to load a bold (with heavy weight) sans-serif font, that would work on any platform Windows, Linux, Mac.

I don't think ImageFont.truetype("ariblk.ttf") will work cross-platform, is it possible to load it with ImageFont.truetype("Arial Black") or, better, ImageFont.truetype("sans-serif;bold") that would work on all platforms?


Solution

  • Looking at the documentation of Pillow's ImageFont module, there's no such an option, no.

    A handy workaround might be to use Matplotlib's font_manager module for that: A module for finding, managing, and using fonts across platforms. Using the FontProperties and findfont, you should get the a valid path to a font with the given properties, which you can then use in the common ImageFont.truetype call.

    Here's a small example, which runs perfectly fine on my Windows machine. Unfortunately, I don't have any other OS nearby to test.

    from matplotlib import font_manager
    from PIL import Image, ImageDraw, ImageFont
    
    font = font_manager.FontProperties(family='sans-serif', weight='bold')
    file = font_manager.findfont(font)
    print(file)
    
    img = Image.new('RGB', (400, 300), (255, 255, 255))
    draw = ImageDraw.Draw(img)
    
    font = ImageFont.truetype(file, 48)
    draw.text((20, 20), 'Hello World', font=font, fill=(255, 0, 0))
    
    img.save('test.png')
    

    The print output:

    ...\Lib\site-packages\matplotlib\mpl-data\fonts\ttf\DejaVuSans-Bold.ttf
    

    The image output:

    Output

    ----------------------------------------
    System information
    ----------------------------------------
    Platform:      Windows-10-10.0.16299-SP0
    Python:        3.9.1
    Matplotlib:    3.3.4
    Pillow:        8.1.0
    ----------------------------------------