[Image-SIG] PIL method for loading TrueType/OpenType fonts fromfont names?
Fredrik Lundh
fredrik at pythonware.com
Sun Aug 28 22:13:44 CEST 2005
Pascal Charbonneau wrote:
> One thing that is definately missing from the PIL ImageFont module is a
> way to load fonts by font names. I have not found any easy way to
> associate the file name (needed to load a TrueType font in PIL) with the
> actual font typeface name.
that's because there's no portable way to do that, without actually looking
inside all available truetype files.
> i.e "Times New Roman Bold (TrueType)" = "TIMESDB.TTF"
>
> Every standard "font" dialog lets the user choose a Typeface, Point
> Size, etc and associating them to be used with PIL seems to be an issue.
> Unless I am being dumb and havent found the obvious easy solution :)
>
> Anyone has encountered this issue and can suggest a solution?
you have to maintain your own "name to file" mapping, either by querying
a system font manager of some kind, or by scanning the available files, and
extracting the font name and style from the font object:
for file in glob.glob("*.ttf"):
font = ImageFont.truetype(file, 0)
name, style = font.getname()
print name, style, "=>", file
(to avoid spending lots of time on this every time you start the program,
you
probably want to maintain a persistent font cache of some kind. storing the
data in a marshalled dict should be good enough)
</F>
More information about the Image-SIG
mailing list