2

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
SShah
  • 1,044
  • 8
  • 19
  • that's very suprising that using `"C:\Windows\Fonts\ariblk.ttf"` works since `\a` is interpreted. Secondly, try to check `font_size`. Not all sizes are available. Try a standard value like 12 or 10. – Jean-François Fabre Jun 04 '17 at 20:29
  • Wow, I have for the first time used this site to ask a question and was surprised, with how quickly you replied (litterally just clicked send and you replied!!!.. Awesomee!!,,), thank you very much. I will just try your solution of the font sizes and reply back – SShah Jun 04 '17 at 20:35
  • I tried both font sizes and still get the same error, so I am sure its nothing to do with the font size, it has to be due to names with spaces on them, i guess, coz mainly only the font names with spaces produce this error, but I am not sure, might be something entirely different. Also sorry but yea "C:\Windows\Fonts\ariblk.ttf" works fine i found the solution for it at: https://stackoverflow.com/questions/1815165/draw-bold-italic-text-with-pil/1815171, i wanted to comment there to ask but it said i didnt have enough reputation. Thankyou – SShah Jun 04 '17 at 20:41
  • 2
    Your using the wrong file name for Arial Bold. Try `font = ImageFont.truetype(r"C:\Windows\Fonts\arialbd.ttf", font_size)`. The `font = ImageFont.truetype("C:\Windows\Fonts\\ariblk.ttf", font_size)` presumably works because you **do** have Arial Black because that's the name of its `.ttf` font file. – martineau Jun 04 '17 at 22:13
  • Thank you for the reply @martineau, as i mentioned on my original post the path "C:\Windows\Fonts\arialbd.ttf" works, regardless of whether i put r before the speech mark or not, however the issue is the following path which i can physically access on my windows 10 shows me the above error: "C:/Windows/Fonts/Arial/Arial Bold.ttf". Also on my previous reply i meant to say that on the Fonts folder within windows theres just Arial as a folder, no file specifically called ariblk.tff, which is why i was shocked how that path works, but the other one doesnt. – SShah Jun 04 '17 at 23:16
  • The path to the font file is virtual—something that only works in Explorer. Right-click on the font displayed there and select "Properties", then it will show you the actual font filename. – martineau Jun 05 '17 at 01:41
  • You can also see the actual ttf font filenames by entering `dir "c:\windows\fonts\*.ttf"` at the `cmd` prompt (capitalization doesn't matter on Windows). – martineau Jun 05 '17 at 01:49
  • 1
    @martineau, Thankyou very much, this has been the solution i was seeking for, I never even imagined that windows would use different file names within the fonts folder., Both the above replies are both helpful and solve my issue, i have tested a few of the font names I found from viewing the directory of the font folder in command prompt, and they all work as expected, Thank you once again. – SShah Jun 05 '17 at 11:44
  • You're welcome—and there's no need to apologize for being confused. What Explorer does is seamlessly switch to the Windows Fonts Control Panel when you attempt to view the contents of this folder. – martineau Jun 05 '17 at 13:38
  • Hi thankyou once again, I understand now, so windows by default loads the Fonts Control Panel when u try to manually access the folder and therefore the names are not matching to the actual filename. If you dont mind can i ask a follow up question, because some of the orignal font.ttf file names, are confusing to understant, is there a way i can extract the names on the Fonts control panel which then i can make them automatically link to the orignal font.tff file, because this is just a project i am working on for fun, and want to try my best to perfect it. Thanks – SShah Jun 05 '17 at 14:29

1 Answers1

0

I had this same issue and just found out how to fix it since it was bothering me.

I used to be able to just install a font and use this path in Python:
"C:/Windows/Fonts/ARCHRISTY.ttf"

But i guess Windows doesn't actually install the ttf there anymore. I went into the "C:/Windows/Fonts/" folder and right clicked the "AR CHRISTY Medium" font and checked its properties.

Its real location is now at:
".../AppData/Local/Microsoft/Windows/Fonts/ARCHRISTY.ttf"

In Python you can do this to create the path:
os.path.expandvars("%LocalAppData%/Microsoft/Windows/Fonts/ARCHRISTY.ttf")

Puddle
  • 2,993
  • 1
  • 19
  • 32