简体   繁体   English

在PIL中导入base64编码的字体

[英]importing base64 encoded fonts in PIL

I am trying to load a true type font in Pil from a base64 encoded string, with this code: 我正在尝试使用以下代码从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)

but i am getting this error: 但我收到此错误:

SyntaxError: Not a PILfont file 语法错误:不是PILfont文件

i want to include all the font/images in my project in a resource.py file as base64 encoded string resources. 我想将我项目中的所有字体/图像都包含在resource.py文件中,作为base64编码的字符串资源。 I already did it with all the icons and images, but i cant find a way to this with fonts Is there any way to do it? 我已经用所有图标和图像做到了,但是我找不到用字体做到这一点的方法吗?有没有办法做到这一点?

PS: i am using python 3.4.4/windows 10 and Pillow 3.4.2 and i am using notepad++ to encode the ttf file. PS:我正在使用python 3.4.4 / windows 10和Pillow 3.4.2,并且我正在使用notepad ++编码ttf文件。

As in my comment, the _load_pilfont_data function loads a PILFont data (and not a data of TTF font file), so if you try to use that function to load a TTF font - you will get a SyntaxError (because it's not a PILfont file). 如我的评论所述, _load_pilfont_data函数加载PILFont数据(而不是TTF字体文件的数据),因此,如果您尝试使用该函数加载TTF字体-您将收到SyntaxError(因为它不是PILfont文件) 。

What you can do - is use the truetype function of the ImageFont module: 您可以做的-使用ImageFont模块的truetype函数:

import base64
from PIL import ImageFont
from io import BytesIO

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

In the documentation is said you should provide the path to the file, but it seems like a BytesIO also works. 文档中说您应该提供文件的路径,但是BytesIO似乎也可以使用。

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

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