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:

  1. Load TrueType and OpenType font files
  2. Create font objects and set font size
  3. Calculate text size and layout in images
  4. andImageDrawCooperate 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 path
size: Font size (pixels)
index: Font index
encoding: Encoding
layout_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 measure
direction: Text direction
features: OpenType features
language: 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')

Get Text Size

Example

text = "Sample Text"
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'])

Multilingual 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

  1. Font file path: Ensure the provided font file path is correct, otherwise an exception will be thrown.IOError
  2. Font size: Font size is in pixels, not points (pt).
  3. Performance considerations: Frequent creation of font objects can affect performance; it is recommended to reuse font objects.
  4. 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.

Other Extensions