是否可以将在线字体文件链接到 PIL ImageFont.truetype()?

Her*_*roo 0 python python-imaging-library

不要在本地下载字体并将其链接到 ImageFont.truetyp() ,如下所示:

from pillow import ImageFont
font = ImageFont.truetype('Roboto-Regular.ttf', size=10)
Run Code Online (Sandbox Code Playgroud)

我可以做这样的事情吗:

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

Pet*_*ood 8

ImageFont.true_type接受一个类似文件的对象。

Python 的标准库,urllib.request.urlopen返回一个类似文件的对象。

以下应该有效:

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)
Run Code Online (Sandbox Code Playgroud)

编辑:正如 @flyer2403回答的那样,要使该特定网址起作用,您需要添加?raw=true到末尾。