https://www.jianshu.com/p/c64ae3e9b196html
pillow使用备忘之OSError: cannot open resource错误处理python
在使用pillow过程当中,Python程序出现了:OSError: cannot open resource
提示。
web
File "E:\06_python\python_demo\MyBlog\web\views\account.py", line 89, in test2 img, code = create_validate_code() File "E:\06_python\python_demo\MyBlog\utils\check_code.py", line 92, in create_validate_code strs = create_strs() File "E:\06_python\python_demo\MyBlog\utils\check_code.py", line 80, in create_strs font = ImageFont.truetype(font_type, font_size) File "C:\Users\ic\AppData\Local\Programs\Python\Python35\lib\site-packages\PIL\ImageFont.py", line 280, in truetype return FreeTypeFont(font, size, index, encoding, layout_engine) File "C:\Users\ic\AppData\Local\Programs\Python\Python35\lib\site-packages\PIL\ImageFont.py", line 145, in __init__ layout_engine=layout_engine) OSError: cannot open resource [11/May/2019 15:07:57] "GET /test2.html HTTP/1.1" 500 92941 Performing system checks...
经查此类是常见的字体问题。
在Windows环境,字体通常位于C:\WINDOWS\Fonts
文件夹下。用户能够到此文件夹中查看Python程序中指定的字体是否存在。
字体
实践出真知:
字体名称英文大小写要一致;
字体名称是英文的,不是中文。
如简体黑体常规程序中对应的字体名称为simhei.ttf
,而简体仿宋常规对应的字体名为simfang.ttf
。
Python程序中直接写相似华文行楷.ttf、微软简粗黑.TTF等中文名称的字体会出错。spa
查看对应字体英文名称的方法很简单,咱们能够选中相应字体,右击属性项便可找到。
code
STXINWEI.TTF
,这个英文名用于Python程序才正确。
以上是我实践的结论,能够供临时解决上述出错问题。
但出错背后的机制,我仍未掌握,欢迎高手交流指正。orm