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 the:commandpilfont
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
- from PIL import ImageFont, ImageDraw
- draw = ImageDraw.Draw(image)
- # use a bitmap font
- font = ImageFont.load("arial.pil")
- 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)[源代码]- 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='')[源代码]- Load a TrueType or OpenType font file, and create a font object.This function loads a font object from the given file, and createsa font object for a font of the given size.
This function requires the _imagingft service.
参数:
- font – A truetype font file. Under Windows, if the fileis not found in this filename, the loader also looks inWindows
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.返回:
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
返回:(width, height)
If the font uses antialiasing, the bitmap should have mode “L” and use amaximum value of 255. Otherwise, it should have mode “1”.
参数:
- text – Text to render.
- mode –
Used by some graphics drivers to indicate what mode thedriver prefers; if empty, the renderer may return eithermode. Note that the mode is always a string, to simplifyC-level implementations.
1.1.5 新版功能.
返回:
An internal PIL storage memory instance as defined by thePIL.Image.core
interface module.