How can I load a font file using PIL.ImageFont.truetype without specifying an absolute path?

When I write code in Windows, this code can very easily download a font file:

ImageFont.truetype(filename='msyhbd.ttf', size=30); 

I assume that the font location is registered in the Windows registry. But when I go over the code in Ubuntu and copy the font file to / usr / share / fonts /, the code cannot find the font:

  self.font = core.getfont(font, size, index, encoding) IOError: cannot open resource 

How can I get a PIL to search for a ttf file without specifying an absolute path?

+6
source share
4 answers

According to the PIL documentation, only the Windows font directory is searched:

On Windows, if the name of the file does not exist, the bootloader also scans the Windows fonts directory.

http://effbot.org/imagingbook/imagefont.htm

So, you need to write your own code to find the full path in Linux.

However, Pillow, a PIL fork, currently has a PR for searching the Linux directory. It is not yet clear which directories all Linux variants will look for, but you can see the code here and possibly contribute to PR:

https://github.com/python-pillow/Pillow/pull/682

+6
source

I succeeded on xubuntu:

 from PIL import Image,ImageDraw,ImageFont # sample text and font unicode_text = u"Hello World!" font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeMono.ttf", 28, encoding="unic") # get the line size text_width, text_height = font.getsize(unicode_text) # create a blank canvas with extra space between lines canvas = Image.new('RGB', (text_width + 10, text_height + 10), "orange") # draw the text onto the text canvas, and use black as the text color draw = ImageDraw.Draw(canvas) draw.text((5,5), u'Hello World!', 'blue', font) # save the blank canvas to a file canvas.save("unicode-text.png", "PNG") canvas.show() 

enter image description here

Windows version

 from PIL import Image, ImageDraw, ImageFont unicode_text = u"Hello World!" font = ImageFont.truetype("arial.ttf", 28, encoding="unic") text_width, text_height = font.getsize(unicode_text) canvas = Image.new('RGB', (text_width + 10, text_height + 10), "orange") draw = ImageDraw.Draw(canvas) draw.text((5, 5), u'Hello World!', 'blue', font) canvas.save("unicode-text.png", "PNG") canvas.show() 

The output is the same as above.

+7
source

On mac, I just copy the Arial.ttf font file into the project directory and everything works.

+3
source

There is a Python fontconfig where you can access the system font configuration. The code sent by Jeeg_robot can be changed as follows:

 from PIL import Image,ImageDraw,ImageFont import fontconfig # find a font file fonts = fontconfig.query(lang='en') for i in range(1, len(fonts)): if fonts[i].fontformat == 'TrueType': absolute_path = fonts[i].file break # the rest is like the original code: # sample text and font unicode_text = u"Hello World!" font = ImageFont.truetype(absolute_path, 28, encoding="unic") # get the line size text_width, text_height = font.getsize(unicode_text) # create a blank canvas with extra space between lines canvas = Image.new('RGB', (text_width + 10, text_height + 10), "orange") # draw the text onto the text canvas, and use black as the text color draw = ImageDraw.Draw(canvas) draw.text((5,5), u'Hello World!', 'blue', font) # save the blank canvas to a file canvas.save("unicode-text.png", "PNG") canvas.show() 
+1
source

Source: https://habr.com/ru/post/970466/


All Articles