栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

.ttf OSError: cannot open resource

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.ttf OSError: cannot open resource

遇到这个问题,在网上找了一天也没找到准确的解决方法,通过大量测试,发现了关键点,所以记录下来,让后面遇到相同问题的人一些思路,避免进坑。

在图上绘上字符,可以使用如下的方法,但是需要特点的字体时,需要在网上的字体网站下载相关的字体,一般都是ttf格式,然后把它复制到c:/windows/fonts里面,会自动完成安装。但是安装后,选择文件,右键属性看他的地址。

from PIL import ImageFont, Image, ImageDraw


fontpath="C:\Users\Administrator\AppData\Local\Microsoft\Windows\Fonts\Creepster.ttf"
font = ImageFont.truetype(fontpath, size=600, index=0) 
img_pil = Image.fromarray(img_temp)
draw = ImageDraw.Draw(img_pil)
draw.text((50, -50), str, font=font, fill=(255, 255, 255))

运行上面的代码,字体已经修改为自己下载的字体(在此觅知网站可下载,需要收费),就会出现下面的错误,使用默认的字体路径,不会出现,比如:fontpath="C:\Windows\Fonts\simsun.ttc"

提示的错误如下:

    font = ImageFont.truetype(fontpath, size=300, index=0)
  File "C:UsersAdministratoranaconda3envsenv_mlibsite-packagesPILImageFont.py", line 878, in truetype
    return freetype(font)
  File "C:UsersAdministratoranaconda3envsenv_xlibsite-packagesPILImageFont.py", line 875, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "C:UsersAdministratoranaconda3envsenv_xlibsite-packagesPILImageFont.py", line 227, in __init__
    font, size, index, encoding, layout_engine=layout_engine
OSError: cannot open resource

解决方法,其实很简单:

只要Creepster.ttf"  修改为  creepster.ttf  ,  首字母要小写。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/951651.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号