My question is related to Python, specifically the Python PIL library. I know there are several questions similar to mine, unfortunately I am unable to reply/comment on those questions because my reputation is not high enough. My issue is specific to choosing a windows font for PIL:
Below is my code for what font I want to select:
Based on reading other solutions on stack overflow to get Arial Bold font, I found a solution which stated to add the following code to get arial bold to work:
font = ImageFont.truetype("C:\Windows\Fonts\\ariblk.ttf", font_size)
And this works (Whats weird is I don't even see ariblk.tff
as an option within C:\Windows\Fonts
directory path, but this works.). However, I am not sure why, the path below which exists, shows me the following error when I run it:
font = ImageFont.truetype("C:/Windows/Fonts/Arial/Arial Bold.ttf", font_size)
ERROR:
Traceback (most recent call last):
File "C:\Users\Sagar\Desktop\Python Image Edit Test\imageEdit.py", line 48, in <module>
font = ImageFont.truetype("C:/Windows/Fonts/Arial/Arial Black.ttf", font_size)
File "C:\Users\Sagar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\PIL\ImageFont.py", line 238, in truetype
return FreeTypeFont(font, size, index, encoding)
File "C:\Users\Sagar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\PIL\ImageFont.py", line 127, in __init__
self.font = core.getfont(font, size, index, encoding)
OSError: cannot open resource
Although I have got a solution for the Arial Bold font above, I was asking this question, because there are several fonts within windows, which if I ever wanted to use in the future, I can never, because I keep getting this error. I am specifying the exact directory path to the font.tff
Windows file such as in my second example above which gave me the error.
Currently I feel that I face these errors because the names of this font files have spaces, but I am not 100% sure about that. If anyone can explain or show me why I cant get any Windows font.tff
file to work, I would greatly appreciate it.
Basically within the windows font directory path (C:\Windows\Fonts
) I have noticed, that some fonts can be further opened to view additional fonts, like I show on my 2nd example above which does not work:
Please note: My overall code for using the PIL Library works and outputs the fonts to images just as I like, I just mainly want support with finding out how I can make the PIL library work with any font.tff
file I want.