2014-06-06 10 views
6

Wenn ich den Code in Windows zu schreiben, können Sie diesen Code die Font-Datei laden gerade fein:Wie kann ich eine Schriftartdatei mit PIL.ImageFont.truetype laden, ohne den absoluten Pfad anzugeben?

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

Ich denke, die Schriftart Lage in Windows-Registrierung registriert ist. Aber wenn ich den Code zu Ubuntu bewegen, und über das Verzeichnis/usr/share die Font-Datei kopieren/fonts/der Code die Schriftart nicht finden kann:

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

Wie ich PIL bekommen kann die ttf-Datei zu finden, ohne den absoluten Pfad angeben?

+0

Wenn der font Truetype (.ttf) ist, haben Sie es ausdrückte in das TrueType-Unterverzeichnis in/usr/share/fonts /? Ich würde es jetzt versuchen, aber im Moment habe ich keinen Zugriff auf eine Ubuntu-Maschine. – EML

Antwort

6

Nach der PIL Dokumentation wird nur Windows-Font-Verzeichnis gesucht:

Wenn unter Windows der angegebene Dateiname nicht existiert, auch der Lader in Windows-Fonts-Verzeichnis aussieht.

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

So müssen Sie Ihren eigenen Code schreiben, für den vollständigen Pfad auf Linux zu suchen.

Allerdings hat Pillow, die PIL-Gabel, derzeit eine PR, um ein Linux-Verzeichnis zu durchsuchen. Es ist nicht genau klar, doch die für alle Linux suchen Verzeichnisse Varianten, aber Sie können den Code hier und vielleicht dazu beitragen, die PR zu sehen:

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

7

Für mich war dies auf 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() 

Die Ausgabe ist die gleiche wie oben

+1

Dieser Code arbeitete für mich in Ubuntu, danke! – Adjam

+0

@Adjam gut zu wissen –

3

Auf Mac, kopiere ich einfach die Schriftart-Datei Arial.ttf in das Projektverzeichnis und alles funktioniert.

1

Es gibt eine Python fontconfig Paket, wobei eine Systemschriftart Konfiguration zugreifen können, Der Code von Jeeg_robot posted kann wie so geändert werden:

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() 
Verwandte Themen