Skip to content

Image Utils

Utility functions for image-to-text operations in Gen AI applications.

This module provides a comprehensive set of utility functions for handling image data in various formats and sources. It includes functionality for: 1. Image validation and format checking 2. Image loading from various sources (file, URL, base64, S3) 3. Image format conversion and encoding

combine_strings(texts)

Backward-compatible alias for combine_strings in media_utils.

Deprecated

This function is deprecated and will be removed in 0.4.0. Use combine_strings from gllm_multimodal.utils.media_utils instead.

Parameters:

Name Type Description Default
texts list[str]

A list of strings to combine.

required

Returns:

Name Type Description
str str

The combined string.

Examples:

>>> combine_strings(["hello", "world"])
"hello, world"

convert_into_structured_caption(text)

Convert a structured caption string (YAML or JSON) into a dictionary.

Parameters:

Name Type Description Default
text str

The structured caption string to parse.

required

Returns:

Type Description
dict[str, Any]

dict[str, Any]: The parsed dictionary, or an empty dictionary if parsing fails.

get_image_binary(image_source) async

Backward-compatible alias for get_media_binary.

Deprecated

This function is deprecated and will be removed in 0.4.0. Use get_media_binary from gllm_multimodal.utils.media_utils instead.

Parameters:

Name Type Description Default
image_source Any

The image source to process.

required

Returns:

Type Description
tuple[bytes | None, str | None]

tuple[bytes | None, str | None] | None: The image binary data and MIME type.

Examples:

>>> get_image_binary(b"hello")
(b"hello", "image/jpeg")

get_image_from_base64(image_source)

Backward-compatible alias for get_media_from_base64.

Deprecated

This function is deprecated and will be removed in 0.4.0. Use get_media_from_base64 from gllm_multimodal.utils.media_utils instead.

Parameters:

Name Type Description Default
image_source str

The image source to process.

required

Returns:

Type Description
bytes | None

bytes | None: The image binary data.

Examples:

>>> get_image_from_base64("b'")
b""

get_unique_non_empty_strings(texts)

Backward-compatible alias for get_unique_non_empty_strings in media_utils.

Deprecated

This function is deprecated and will be removed in 0.4.0. Use get_unique_non_empty_strings from gllm_multimodal.utils.media_utils instead.

Parameters:

Name Type Description Default
texts list[str]

A list of strings to process.

required

Returns:

Type Description
list[str]

list[str]: A list of unique non-empty strings.

Examples:

>>> get_unique_non_empty_strings(["hello", "world", "hello"])
["hello", "world"]

is_valid_binary_data(image_binary_data, allowed_mime_types=None)

Backward-compatible alias for is_valid_binary_data in media_utils.

Deprecated

This function is deprecated and will be removed in 0.4.0. Use is_valid_binary_data from gllm_multimodal.utils.media_utils instead.

Parameters:

Name Type Description Default
image_binary_data bytes

The image binary data to validate.

required
allowed_mime_types list[str]

The allowed MIME types. Defaults to None.

None

Returns:

Name Type Description
bool bool

True if the image binary data is valid, False otherwise.

Examples:

>>> is_valid_binary_data(b"hello")
False

resize_attachment(attachment, target_width, target_height, preserve_aspect_ratio=True)

Resize an attachment's image if it is smaller than the target dimensions.

Parameters:

Name Type Description Default
attachment Attachment

The input attachment containing image binary data.

required
target_width int

The minimum target width.

required
target_height int

The minimum target height.

required
preserve_aspect_ratio bool

Whether to preserve the aspect ratio while resizing. Defaults to True.

True

Returns:

Name Type Description
Attachment Attachment

The original attachment if already at or above target dimensions, otherwise a new attachment with the resized image.

resize_image(image_bytes, target_width, target_height, preserve_aspect_ratio=True)

Resize image to meet target dimension requirements.

Parameters:

Name Type Description Default
image_bytes bytes

The input image binary data.

required
target_width int

The target width.

required
target_height int

The target height.

required
preserve_aspect_ratio bool

Whether to preserve the aspect ratio while resizing. If True, the image is scaled to cover the target dimensions while maintaining aspect ratio. If False, the image is resized exactly to the target dimensions. Defaults to True.

True

Returns:

Name Type Description
bytes bytes

The resized image binary data.

Examples:

Resize with aspect ratio preservation (default)

If input is 64x128 and target is 128x128, output will be 128x256

resized_bytes = resize_image(original_bytes, target_width=128, target_height=128)

Resize to exact dimensions without preserving aspect ratio

Output will be exactly 128x128 regardless of input aspect ratio

exact_bytes = resize_image( original_bytes, target_width=128, target_height=128, preserve_aspect_ratio=False )