Pillow ImageFont Module
ImageFontThe module is a core component in Pillow specifically for handling fonts and text rendering.
The ImageFont module allows developers to add text content of various styles to images, and is an essential tool for image annotation, CAPTCHA generation, watermark creation, and other functions.
Core Features
ImageFontThe module mainly provides the following functions:
- Load TrueType and OpenType font files
- Create font objects and set font size
- Calculate text size and layout in images
- and
ImageDrawCooperate with other modules to render text
Detailed Explanation of Main Methods
The following areImageFontthe most commonly used methods in the module and their descriptions:
| Method Name | Parameter Description | Return Value | Function Description |
|---|---|---|---|
truetype(font=None, size=10, index=0, encoding='', layout_engine=None) |
font: Font file pathsize: Font size (pixels)index: Font indexencoding: Encodinglayout_engine: Layout engine |
ImageFontobject |
Load TrueType or OpenType font files |
load(filename) |
filename: Font file path |
ImageFontobject |
Load bitmap font (PIL-specific format) |
load_path(filename) |
filename: Font file path |
ImageFontobject |
Load font file from the specified path |
getsize(text, direction=None, features=None, language=None) |
text: Text to measuredirection: Text directionfeatures: OpenType featureslanguage: Language code |
(width, height) tuple | Get the rendered text size |
getmask(text, mode='', direction=None, features=None, language=None) |
Same as above | Imageobject |
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 Examples
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 drawing object
draw = ImageDraw.Draw(image)
# Draw text
draw.text((50, 80), 'Hello Pillow!', font=font, fill='black')
# Save image
image.save('text_image.png')
# Create a blank image
image = Image.new('RGB', (400, 200), color='white')
# Load font
font = ImageFont.truetype('arial.ttf', size=40)
# Create drawing object
draw = ImageDraw.Draw(image)
# Draw text
draw.text((50, 80), 'Hello Pillow!', font=font, fill='black')
# Save image
image.save('text_image.png')
Get Text Size
Example
text = "Sample Text"
width, height = font.getsize(text)
print(f"Text width: {width}, Text height: {height}")
width, height = font.getsize(text)
print(f"Text width: {width}, Text height: {height}")
Advanced Features
Using OpenType Features
Example
font = ImageFont.truetype('arial.ttf', size=40)
draw.text((50, 80), 'Hello', font=font, fill='black', features=['-liga'])
draw.text((50, 80), 'Hello', font=font, fill='black', features=['-liga'])
Multilingual Support
Example
# Chinese text rendering
font = ImageFont.truetype('msyh.ttc', size=30)
draw.text((50, 120), 'Hello, World!', font=font, fill='blue')
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')
draw.text((300, 80), 'سلام', font=font, fill='red', direction='rtl')
Notes
- Font file path: Ensure the provided font file path is correct, otherwise an exception will be thrown.
IOError - Font size: Font size is in pixels, not points (pt).
- Performance considerations: Frequent creation of font objects can affect performance; it is recommended to reuse font objects.
- Cross-platform compatibility: Different operating systems may have different fonts installed by default; font packaging should be considered during deployment.
Summary
ImageFontThe module provides powerful text rendering capabilities for Python image processing. By using this module appropriately, developers can easily implement various text-related image processing needs. MasteringImageFontits use is an important step for professional-level image processing.