Loading font from URL to Pillow

天大地大妈咪最大 提交于 2021-01-28 18:57:37

问题


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.


回答1:


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)



回答2:


You can
(1) Fetch the font with HTTP GET request, using urllib.request.urlopen()
(2) memoize the result with @functools.lrucache or @memoization.cache so the font is not fetched every time you run the function and
(3) Pass the contents as a file-like object with io.BytesIO

from PIL import ImageFont
import urllib.request
import functools
import io


@functools.lru_cache
def get_font_from_url(font_url):
    return urllib.request.urlopen(font_url).read()


def webfont(font_url):
    return io.BytesIO(get_font_from_url(font_url))


if __name__ == "__main__":
    font_url = "https://github.com/googlefonts/roboto/blob/master/src/hinted/Roboto-Regular.ttf?raw=true"
    with webfont(font_url) as f:
        imgfnt = ImageFont.truetype(f, 15)

There is also python-memoization (pip install memoization) for alternative way of memoizing. Usage would be

from memoization import cache 

@cache
def get_font_from_url(font_url):
    return urllib.request.urlopen(font_url).read()

memoization speed

Without memoization:

In [1]: timeit get_font_from_url(font_url)
The slowest run took 4.95 times longer than the fastest. This could mean that an intermediate result is being cached.
1.32 s ± 1.11 s per loop (mean ± std. dev. of 7 runs, 1 loop each)

With memoization:

In [1]: timeit get_font_from_url(font_url)
The slowest run took 11.00 times longer than the fastest. This could mean that an intermediate result is being cached.
271 ns ± 341 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)
```t


来源:https://stackoverflow.com/questions/64708802/loading-font-from-url-to-pillow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!