Pillow ImageFont Module
ImageFont The module is a core component in Pillow specifically used for handling fonts and text rendering.
The ImageFont module allows developers to add text content of various styles to images. It is an essential tool for image annotation, generating verification codes, creating watermarks and other functions.
Core Features
ImageFont The module mainly provides the following functions:
- Load TrueType and OpenType font files
- Create font objects and set font sizes
- Calculate the size and layout of text in an image
- The difference between
ImageDrawUse with the module to implement text rendering
Detailed explanation of main methods
The following are ImageFont The most commonly used methods in the module and their descriptions:
| Method Name | Parameter Description | Return Value | Description |
|---|---|---|---|
truetype(font=None, size=10, index=0, encoding='', layout_engine=None) |
font: Font file path |
`size`: 字体大小(像素)
`index`: 字体索引
`encoding`: 编码方式
`layout_engine`: 布局引擎 | `ImageFont` 对象 | 加载 TrueType 或 OpenType 字体文件 |
| load(filename) | filename: Font file path | ImageFont Object | Load bitmap font (PIL-specific format) | | load_path(filename) | filename: Font file path | ImageFont Object | Load font file from specified path | | getsize(text, direction=None, features=None, language=None) | text: Text to measure
direction: Text direction
features: OpenType features
language: Language code | (width, height) tuple | Get the size after text rendering | | getmask(text, mode='', direction=None, features=None, language=None) | Same as above | Image Object | Get the bitmap mask of the text | | getbbox(text, direction=None, features=None, language=None) | Same as above | (left, top, right, bottom) tuple | Get the bounding box of the text |
Basic Usage Example
Load font and draw text
Example
from
PIL
import
Image
,
ImageDraw
,
ImageFont
Create a blank image
image
=
Image.
new
(
'RGB'
,
(
400
,
200
)
,
color
=
'white'
)
Load font
font
=
ImageFont.
truetype
(
'arial.ttf'
,
size
=
40
)
Create a drawing object
draw
=
ImageDraw.
Draw
(
image
)
Draw the text
draw.
text
(
(
50
,
80
)
,
'Hello Pillow!'
,
font
=
font
,
fill
=
'black'
)
Save the image
image.
save
(
'text_image.png'
)
Get the text size
Example
text
=
"Sample Text"
width
,
height
=
font.
getsize
(
text
)
(
f
"Text width: {width}, Text height: {height}"
)
Advanced Features
Use OpenType features
Example
font
=
ImageFont.
truetype
(
'arial.ttf'
,
size
=
40
)
draw.
text
(
(
50
,
80
)
,
'Hello'
,
font
=
font
,
fill
=
'black'
,
features
=
[
'-liga'
]
)
Multi-Language Support
Example
Chinese text rendering
font
=
ImageFont.
truetype
(
'msyh.ttc'
,
size
=
30
)
draw.
text
(
(
50
,
120
)
,
'Hello, World!'
,
font
=
font
,
fill
=
'blue'
)
Text direction control
Example
Right-to-left text
draw.
text
(
(
300
,
80
)
,
'سلام'
,
font
=
font
,
fill
=
'red'
,
direction
=
'rtl'
)
Notes
- Font file path: Ensure the provided font file path is correct, otherwise it will raise
IOError - Font size: Font size is in pixels, not points (pt)
- Performance considerations: Frequently creating font objects affects performance; it is recommended to reuse font objects
- Cross-platform compatibility: Different operating systems may have different default fonts installed; consider font packaging during deployment
Summary
ImageFont The module provides powerful text rendering capabilities for Python image processing. By using this module reasonably, developers can easily implement various text-related image processing needs. Mastering ImageFont the use of is an important step toward professional-grade image processing.