ImageFont Module

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

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

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

Example

  1. from PIL import ImageFont, ImageDraw
  2.  
  3. draw = ImageDraw.Draw(image)
  4.  
  5. # use a bitmap font
  6. font = ImageFont.load("arial.pil")
  7.  
  8. draw.text((10, 10), "hello", font=font)
  9.  
  10. # use a truetype font
  11. font = ImageFont.truetype("arial.ttf", 15)
  12.  
  13. draw.text((10, 25), "world", font=font)

Functions

  • PIL.ImageFont.load(filename)[源代码]
  • Load a font file. This function loads a font object from the givenbitmap font file, and returns the corresponding font object.

参数:filename – Name of font file.返回:A font object.引发:IOError – If the file could not be read.

  • PIL.ImageFont.loadpath(_filename)[源代码]
  • Load font file. Same as load(), but searches for abitmap font along the Python path.

参数: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)[源代码]
  • 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-likeobject, and creates a font object for a font of the given size.

This function requires the _imagingft service.

参数:

  • 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). Commonencodings are “unic” (Unicode), “symb” (MicrosoftSymbol), “ADOB” (Adobe Standard), “ADBE” (Adobe Expert),and “armn” (Apple Roman). See the FreeType documentationfor 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()[源代码]
  • Load a “better than nothing” default font.

1.1.4 新版功能.

返回:A font object.

Methods

  • class PIL.ImageFont.ImageFont[源代码]
  • PIL font wrapper
  • class PIL.ImageFont.FreeTypeFont(font=None, size=10, index=0, encoding='', layout_engine=None)[源代码]
  • FreeType font wrapper (requires _imagingft service)

    • fontvariant(_font=None, size=None, index=None, encoding=None, layout_engine=None)[源代码]
    • Create a copy of this FreeTypeFont object,using any specified arguments to override the settings.

Parameters are identical to the parameters used to initialize thisobject.

返回:A FreeTypeFont object.

  • class PIL.ImageFont.TransposedFont(font, orientation=None)[源代码]
  • Wrapper for writing rotated or mirrored text