pillow ImageFont.truetype OSError: cannot open resource

 

from PIL import ImageFont, ImageDraw
# 导入中文字体,指定字体大小
font = ImageFont.truetype('SimHei.ttf', 50)

 

OSError                                   Traceback (most recent call last)
/tmp/ipykernel_4385/1341045620.py in <module>
      1 from PIL import ImageFont, ImageDraw
      2 # 导入中文字体,指定字体大小
----> 3 font = ImageFont.truetype('SimHei.ttf', 50)

/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/PIL/ImageFont.py in truetype(font, size, index, encoding, layout_engine)
   1006 
   1007     try:
-> 1008         return freetype(font)
   1009     except OSError:
   1010         if not is_path(font):

/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/PIL/ImageFont.py in freetype(font)
   1003 
   1004     def freetype(font):
-> 1005         return FreeTypeFont(font, size, index, encoding, layout_engine)
   1006 
   1007     try:

/home/software/anaconda3/envs/tf115/lib/python3.7/site-packages/PIL/ImageFont.py in __init__(self, font, size, index, encoding, layout_engine)
    254                     return
    255             self.font = core.getfont(
--> 256                 font, size, index, encoding, layout_engine=layout_engine
    257             )
    258         else:

OSError: cannot open resource

 

解决方法:

补全字体的路径(绝对路径)。

 font = ImageFont.truetype('/path/to/this/file/SimHei.ttf', 50)

posted @ 2023-02-11 21:04  emanlee  阅读(183)  评论(0编辑  收藏  举报