TextImage

Targets:
image
Image Types:uint8, float32

Apply text rendering transformations on images.

This class supports rendering text directly onto images using a variety of configurations, such as custom fonts, font sizes, colors, and augmentation methods. The text can be placed inside specified bounding boxes.

Arguments
font_path
str | Path

Path to the font file to use for rendering text.

stopwords
tuple[str, ...]
["the","is","in","at","of"]

List of stopwords for text augmentation.

augmentations
tuple[['insertion', 'swap', 'deletion', None], ...]
[null]

List of text augmentations to apply. None: text is printed as is "insertion": insert random stop words into the text. "swap": swap random words in the text. "deletion": delete random words from the text.

fraction_range
tuple[float, float]
[1,1]

Range for selecting a fraction of bounding boxes to modify.

font_size_fraction_range
tuple[float, float]
[0.8,0.9]

Range for selecting the font size as a fraction of bounding box height.

font_color
tuple[float, ...]
[0,0,0]

Font color as RGB values (e.g., (0, 0, 0) for black).

clear_bg
bool
false

Whether to clear the background before rendering text.

metadata_key
str
textimage_metadata

Key to access metadata in the parameters.

p
float
0.5

Probability of applying the transform.

Examples
>>> import albumentations as A
>>> transform = A.Compose([
    A.TextImage(
        font_path=Path("/path/to/font.ttf"),
        stopwords=("the", "is", "in"),
        augmentations=("insertion", "deletion"),
        fraction_range=(0.5, 1.0),
        font_size_fraction_range=(0.5, 0.9),
        font_color=(255, 0, 0),  # red in RGB
        metadata_key="text_metadata",
        p=0.5
    )
])
>>> transformed = transform(image=my_image, text_metadata=my_metadata)
>>> image = transformed['image']
# This will render text on `my_image` based on the metadata provided in `my_metadata`.