albumentations.augmentations.text.functional


Functional implementations for text manipulation and rendering. This module provides utility functions for manipulating text in strings and rendering text onto images. Includes functions for word manipulation, text drawing, and handling text regions in images.

convert_image_to_pilfunction

convert_image_to_pil(
    image: np.ndarray
)

Convert a NumPy array image to a PIL image.

Parameters

NameTypeDefaultDescription
imagenp.ndarray--

delete_random_wordsfunction

delete_random_words(
    words: list[str],
    num_words: int,
    py_random: random.Random
)

Delete a specified number of random words from a list. This function randomly removes words from the input list and joins the remaining words with spaces to form a new string.

Parameters

NameTypeDefaultDescription
wordslist[str]-List of words to process.
num_wordsint-Number of words to delete.
py_randomrandom.Random-Random number generator for reproducibility.

Returns

  • str: New string with specified words removed. Returns empty string if

draw_text_on_multi_channel_imagefunction

draw_text_on_multi_channel_image(
    image: np.ndarray,
    metadata_list: list[dict[str, Any]]
)

Draw text on a multi-channel image with more than three channels.

Parameters

NameTypeDefaultDescription
imagenp.ndarray--
metadata_listlist[dict[str, Any]]--

draw_text_on_pil_imagefunction

draw_text_on_pil_image(
    pil_image: Image,
    metadata_list: list[dict[str, Any]]
)

Draw text on a PIL image.

Parameters

NameTypeDefaultDescription
pil_imageImage--
metadata_listlist[dict[str, Any]]--

inpaint_text_backgroundfunction

inpaint_text_background(
    image: np.ndarray,
    metadata_list: list[dict[str, Any]],
    method: int = 1
)

Inpaint (clear) regions in an image where text will be rendered. This function creates a clean background for text by inpainting rectangular regions specified in the metadata. It removes any existing content in those regions to provide a clean slate for rendering text.

Parameters

NameTypeDefaultDescription
imagenp.ndarray-Image to inpaint.
metadata_listlist[dict[str, Any]]-List of metadata dictionaries containing: - bbox_coords: Bounding box coordinates (x_min, y_min, x_max, y_max)
methodint1Inpainting method to use. Defaults to cv2.INPAINT_TELEA. Options include: - cv2.INPAINT_TELEA: Fast Marching Method - cv2.INPAINT_NS: Navier-Stokes method

Returns

  • np.ndarray: Image with specified regions inpainted.

insert_random_stopwordsfunction

insert_random_stopwords(
    words: list[str],
    num_insertions: int,
    stopwords: tuple[str, ...] | None,
    py_random: random.Random
)

Insert random stopwords into a list of words. This function randomly inserts stopwords at random positions in the list of words a specified number of times.

Parameters

NameTypeDefaultDescription
wordslist[str]-List of words to process.
num_insertionsint-Number of stopwords to insert.
stopwords
One of:
  • tuple[str, ...]
  • None
-Tuple of stopwords to choose from. If None, default stopwords will be used.
py_randomrandom.Random-Random number generator for reproducibility.

Returns

  • str: New string with stopwords inserted.

render_textfunction

render_text(
    image: np.ndarray,
    metadata_list: list[dict[str, Any]],
    clear_bg: bool
)

Render text onto an image based on provided metadata. This function draws text on an image using metadata that specifies text content, position, font, and color. It can optionally clear the background before rendering. The function handles different image types (grayscale, RGB, multi-channel).

Parameters

NameTypeDefaultDescription
imagenp.ndarray-Image to draw text on.
metadata_listlist[dict[str, Any]]-List of metadata dictionaries containing: - bbox_coords: Bounding box coordinates (x_min, y_min, x_max, y_max) - text: Text string to render - font: PIL ImageFont object - font_color: Color for the text
clear_bgbool-Whether to clear (inpaint) the background under the text.

Returns

  • np.ndarray: Image with text rendered on it.

swap_random_wordsfunction

swap_random_words(
    words: list[str],
    num_words: int,
    py_random: random.Random
)

Swap random pairs of words in a list of words. This function randomly selects pairs of words and swaps their positions a specified number of times.

Parameters

NameTypeDefaultDescription
wordslist[str]-List of words to process.
num_wordsint-Number of swaps to perform.
py_randomrandom.Random-Random number generator for reproducibility.

Returns

  • str: New string with words swapped. If num_words is 0 or the list has fewer