繁体   English   中英

在PIL中导入base64编码的字体

[英]importing base64 encoded fonts in PIL

我正在尝试使用以下代码从base64编码的字符串中加载Pil中的真型字体:

import base64
from PIL import ImageFont
from io import BytesIO

TTF = "T1RUTwAJAIAAAwAQQ0ZGIGkSYyMAAACcAA"  # ...this is obviously only part of the string representing a base64 encoded ttf file

fontub = ImageFont.ImageFont()
fontub._load_pilfont_data(BytesIO(base64.b64decode(TTF)),None)

但我收到此错误:

语法错误:不是PILfont文件

我想将我项目中的所有字体/图像都包含在resource.py文件中,作为base64编码的字符串资源。 我已经用所有图标和图像做到了,但是我找不到用字体做到这一点的方法吗?有没有办法做到这一点?

PS:我正在使用python 3.4.4 / windows 10和Pillow 3.4.2,并且我正在使用notepad ++编码ttf文件。

如我的评论所述, _load_pilfont_data函数加载PILFont数据(而不是TTF字体文件的数据),因此,如果您尝试使用该函数加载TTF字体-您将收到SyntaxError(因为它不是PILfont文件) 。

您可以做的-使用ImageFont模块的truetype函数:

import base64
from PIL import ImageFont
from io import BytesIO

TTF = "T1RUTwAJAIAAAwAQQ0ZGIGkSYyMAAACcAA" 
fontub = ImageFont.truetype(BytesIO(base64.b64decode(TTF)))

文档中说您应该提供文件的路径,但是BytesIO似乎也可以使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM