Search code examples
pythonpython-3.xpython-imaging-librarygoogle-colaboratory

Loading font from URL to Pillow


Is there a way to load a font with Pillow library directly from url, preferably into Google Colab? I tried something like

from PIL import Image, ImageDraw, ImageFont ImageFont.truetype("https://github.com/googlefonts/roboto/blob/master/src/hinted/Roboto-Regular.ttf?raw=true", 15)

Yet I get an error of OSError: cannot open resource. I tried with Google Fonts too, but to no avail. enter image description here


Solution

  • Try it like this:

    import requests
    from io import BytesIO
    
    req = requests.get("https://github.com/googlefonts/roboto/blob/master/src/hinted/Roboto-Regular.ttf?raw=true")
    
    font = ImageFont.truetype(BytesIO(req.content), 72)