ImageFont Module¶

The ImageFont module defines a class with the same name. Instances of this class store bitmap fonts, and are used with the PIL.ImageDraw.Draw.text() method.

PIL uses its own font file format to store bitmap fonts. You can use the pilfont utility to convert BDF and PCF font descriptors (X window font formats) to this format.

Starting with version 1.1.4, PIL can be configured to support TrueType and OpenType fonts (as well as other font formats supported by the FreeType library). For earlier versions, TrueType support is only available as part of the imToolkit package

Example¶

from PIL import ImageFont, ImageDraw

draw = ImageDraw.Draw(image)

# use a bitmap font

draw.text((10, 10), "hello", font=font)

# use a truetype font
font = ImageFont.truetype("arial.ttf", 15)

draw.text((10, 25), "world", font=font)


Functions¶

PIL.ImageFont.load(filename)[source]

Load a font file. This function loads a font object from the given bitmap font file, and returns the corresponding font object.

Parameters: filename – Name of font file. A font object. IOError – If the file could not be read.
PIL.ImageFont.load_path(filename)[source]

Load font file. Same as load(), but searches for a bitmap font along the Python path.

Parameters: filename – Name of font file. A font object. IOError – If the file could not be read.
PIL.ImageFont.truetype(font=None, size=10, index=0, encoding='', layout_engine=None)[source]

Load a TrueType or OpenType font from a file or file-like object, and create a font object. This function loads a font object from the given file or file-like object, and creates a font object for a font of the given size.

This function requires the _imagingft service.

Parameters: font – A filename or file-like object containing a TrueType font. Under Windows, if the file is not found in this filename, the loader also looks in Windows fonts/ directory. size – The requested size, in points. index – Which font face to load (default is first available face). encoding – Which font encoding to use (default is Unicode). Common encodings are “unic” (Unicode), “symb” (Microsoft Symbol), “ADOB” (Adobe Standard), “ADBE” (Adobe Expert), and “armn” (Apple Roman). See the FreeType documentation for more information. layout_engine – Which layout engine to use, if available: ImageFont.LAYOUT_BASIC or ImageFont.LAYOUT_RAQM. A font object. IOError – If the file could not be read.
PIL.ImageFont.load_default()[source]

Load a “better than nothing” default font.

New in version 1.1.4.

Returns: A font object.

Methods¶

PIL.ImageFont.ImageFont.getsize(text)
Returns: (width, height)
PIL.ImageFont.ImageFont.getmask(text, mode='', direction=None, features=[])

Create a bitmap for the text.

If the font uses antialiasing, the bitmap should have mode “L” and use a maximum value of 255. Otherwise, it should have mode “1”.

Parameters: text – Text to render. mode – Used by some graphics drivers to indicate what mode the driver prefers; if empty, the renderer may return either mode. Note that the mode is always a string, to simplify C-level implementations. New in version 1.1.5. direction – Direction of the text. It can be ‘rtl’ (right to left), ‘ltr’ (left to right) or ‘ttb’ (top to bottom). Requires libraqm. New in version 4.2.0. features – A list of OpenType font features to be used during text layout. This is usually used to turn on optional font features that are not enabled by default, for example ‘dlig’ or ‘ss01’, but can be also used to turn off default font features for example ‘-liga’ to disable ligatures or ‘-kern’ to disable kerning. To get all supported features, see https://docs.microsoft.com/en-us/typography/opentype/spec/featurelist Requires libraqm. New in version 4.2.0. An internal PIL storage memory instance as defined by the PIL.Image.core interface module.