Skip to content
Advertisement

Is possible to link an online font file to PIL ImageFont.truetype()?

Instead of downloading the font locally and link it to ImageFont.truetyp() to be like this:

from pillow import ImageFont
font = ImageFont.truetype('Roboto-Regular.ttf', size=10)

Can I do something like this:

from pillow import ImageFont
font = ImageFont.truetype('https://github.com/googlefonts/roboto/blob/master/src/hinted/Roboto-Regular.ttf', size=10)

Advertisement

Answer

ImageFont.true_type takes a file-like object.

Python’s standard library, urllib.request.urlopen returns a file-like object.

The following should work:

from pillow import ImageFont
from urllib.request import urlopen

truetype_url = 'https://github.com/googlefonts/roboto/blob/master/src/hinted/Roboto-Regular.ttf?raw=true'
font = ImageFont.truetype(urlopen(truetype_url), size=10)

edit: As @flyer2403 answered, to make that particular url work you need to add ?raw=true to the end.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement