![](/img/trans.png)
[英]How I can load a font file with PIL.ImageFont.truetype without specifying the absolute path?
[英]unknown file format when load the otf file using PIL.ImageFont.truetype
我正在使用 PIL 模块加载字体文件。 但出现错误:未知文件格式。
我加载的是一个otf
文件,我正在使用PIL.ImageFont.truetype
函数。 应该没问题,但没有。
我搜索了有关 PLT 的文档,我确定该功能是正确的。
from PIL import Image, ImageDraw, ImageFont
fontsize = 50
font = ImageFont.truetype('./NotoSansCJKjp-Regular.otf', fontsize, encoding='utf-8')
我期待一个字体对象,但实际是:
OSError Traceback (most recent call last)
<ipython-input-4-48a2319a31ba> in <module>
9 #!del NotoSansCJKjp-hinted.zip
10
---> 11 font = ImageFont.truetype('./NotoSansCJKjp-Regular.otf', fontsize, encoding='utf-8')
12 #font = ImageFont.load('NotoSansCJKjp-Regular.otf')
~\.conda\envs\for_kaggle\lib\site-packages\PIL\ImageFont.py in truetype(font, size, index, encoding, layout_engine)
544
545 try:
--> 546 return freetype(font)
547 except IOError:
548 if not isPath(font):
~\.conda\envs\for_kaggle\lib\site-packages\PIL\ImageFont.py in freetype(font)
541
542 def freetype(font):
--> 543 return FreeTypeFont(font, size, index, encoding, layout_engine)
544
545 try:
~\.conda\envs\for_kaggle\lib\site-packages\PIL\ImageFont.py in __init__(self, font, size, index, encoding, layout_engine)
159 return
160 self.font = core.getfont(
--> 161 font, size, index, encoding, layout_engine=layout_engine
162 )
163 else:
OSError: unknown file format
请在之前包含以下代码行
----**font = ImageFont.truetype('./NotoSansCJKjp-Regular.otf', fontsize, encoding='utf-8')**
!wget -q --show-progress https://noto-website-2.storage.googleapis.com/pkgs/NotoSansCJKjp-hinted.zip
!unzip -p NotoSansCJKjp-hinted.zip NotoSansCJKjp-Regular.otf > NotoSansCJKjp-Regular.otf
!rm NotoSansCJKjp-hinted.zip
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.