To specify fonts for the .text() method from
the ImageDraw module, import this
module:
import ImageFont
You can then create a font object from any TrueType font by using this function:
ImageFont.truetype(file,size)
Returns a font object representing the TrueType font
whose file name is given by the argument, with a font
height of file pixels.
size
Methods on font objects include:
.getsize(text)
For a given string , returns a tuple text( where w,h) is the width in pixels that
text will occupy on the display, and w its the
height in pixels.
h
On Unix systems locally, TrueType fonts can be found in this directory:
/usr/share/fonts/
At this writing, there were two families of public-domain fonts:
Deja Vu fonts: /usr/share/fonts/dejavu-lgc/.
Liberation fonts: /usr/share/fonts/liberation/.
Here's a complete program that creates a 200x50 gray image,
writes text on it in red, and saves it to file
runaway.jpg. File
DejaVuLGCSansCondensed-Bold.ttf is
sans-serif, condensed, bold font in the Deja Vu family.
#!/usr/local/bin/python import Image import ImageDraw import ImageFont fontPath = "/usr/share/fonts/dejavu-lgc/DejaVuLGCSansCondensed-Bold.ttf" sans16 = ImageFont.truetype ( fontPath, 16 ) im = Image.new ( "RGB", (200,50), "#ddd" ) draw = ImageDraw.Draw ( im ) draw.text ( (10,10), "Run awayyyy!", font=sans16, fill="red" ) im.save ( "runaway.jpg" )
On Windows systems, look in
C:\WINDOWS\Fonts.