[英]PIL.ImageFont cannot open font with space in name
sI am trying to use PIL.ImageFont.truetype to open a font This is for an OCR project我正在尝试使用 PIL.ImageFont.truetype 打开字体 这是用于 OCR 项目
I have been unable to find a solution in Google or this site so far.到目前为止,我一直无法在 Google 或此站点中找到解决方案。 The problem occurs when I try to open a font with spaces in the name.当我尝试打开名称中带有空格的字体时会出现问题。 Any font name without a space works fine任何没有空格的字体名称都可以正常工作
from PIL import ImageFont
#This Works
ImageFont.truetype("C:\Windows\Fonts\Arial.ttf", 12)
#This does not
ImageFont.truetype("C:\Windows\Fonts\Times New Roman.ttf", 12)
Error is here:错误在这里:
File "B:\\Python\\lib\\site-packages\\PIL\\ImageFont.py", line 145, in init layout_engine=layout_engine)文件“B:\\Python\\lib\\site-packages\\PIL\\ImageFont.py”,第 145 行,在init layout_engine=layout_engine 中)
OSError: cannot open resource OSError:无法打开资源
I've tried the following我试过以下
1) renaming the file (not an elegant solution anyway) 1)重命名文件(无论如何都不是一个优雅的解决方案)
2) moving the font to the same directory as the python script 2)将字体移动到与python脚本相同的目录
3) adding / inside space (suggested) 3)添加/内部空间(建议)
I've also checked the error by trying multiple times with different font names and the same errors occur.我还通过使用不同的字体名称多次尝试来检查错误,并且发生了相同的错误。
so far nothing works到目前为止没有任何效果
Thanks in advance提前致谢
Ok,好的,
I may have a solution.我可能有解决办法。 The hint came to me when I was typing in the font file name and a context menu appeared.当我输入字体文件名并出现上下文菜单时,提示出现了。
In the context menu was a list of fonts.在上下文菜单中有一个字体列表。 All the font names were changed to a single name.所有字体名称都更改为一个名称。 For example "Time New Roman" was changed to "times"例如“Time New Roman”改为“times”
I would be curious to know where this list exists.我很想知道这个列表在哪里。 I tried to find it but came up empty.我试图找到它,但空空如也。
Also, In addition I wanted to work with a custom font that wasn't listed.此外,此外,我想使用未列出的自定义字体。 After downloading the font, I renamed it to a single word font.下载字体后,我将其重命名为单字字体。 Then I placed the font in the same directory as my python script.然后我将字体放在与我的 python 脚本相同的目录中。 This worked OK.这工作正常。
I thought I would post this in case anyone else had the same issue我想我会发布这个以防其他人遇到同样的问题
This is not the string you think it is:这不是您认为的字符串:
"C:\Windows\Fonts\Times New Roman.ttf"
Backslash is an escape character.反斜杠是转义字符。 You need to escape the backslash to have a real backslash, like this:您需要转义反斜杠才能获得真正的反斜杠,如下所示:
"C:\\Windows\\Fonts\\Times New Roman.ttf"
You may also do this, if you prefer:如果您愿意,也可以这样做:
os.path.join('C:\\', 'Windows', 'Fonts', 'Times New Roman.ttf')
I found a list online with Font Names mapping to File Name.我在网上找到了一个列表,其中字体名称映射到文件名。 You can use the file name for the corresponding Font Name.您可以使用文件名作为相应的字体名称。
For Example: for "Times New Roman" we can use "Times.ttf"例如:对于“Times New Roman”,我们可以使用“Times.ttf”
Please find the complete list in the below link请在以下链接中找到完整列表
https://docs.microsoft.com/en-us/typography/fonts/windows_10_font_list https://docs.microsoft.com/en-us/typography/fonts/windows_10_font_list
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.