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)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2022-02-11 Python入门示例系列30 Python运算符优先级和结合性
2022-02-11 Python入门示例系列32 Turtle 入门
2010-02-11 Visual Studio 2005 查找和替换窗口 显示不了