data_designer.config.utils.image_helpers
data_designer.config.utils.image_helpers
data_designer.config.utils.image_helpers
Helper utilities for working with images.
IMAGE_FORMAT_MAGIC_BYTES
_PIL_FORMAT_TO_IMAGE_FORMAT
_BASE64_PATTERN
IMAGE_DIFFUSION_MODEL_PATTERNS
SUPPORTED_IMAGE_EXTENSIONS
Bases: data_designer.config.utils.type_helpers.StrEnum
Supported image formats for image modality.
Initialization:
Initialize self. See help(type(self)) for accurate signature.
Return True if the model is a diffusion-based image generation model.
Parameters:
Model name or identifier (e.g. from provider).
Returns:
bool
True if the model is detected as diffusion-based, False otherwise.
Extract base64 from data URI or return as-is.
Handles data URIs like “data:image/png;base64,iVBORw0…” and returns just the base64 portion.
Parameters:
Data URI (e.g., “data:image/png;base64,XXX”) or plain base64
Returns:
str
Base64 string without data URI prefix
Raises:
If data URI format is invalid
Decode base64 string to image bytes.
Automatically handles data URIs by extracting the base64 portion first.
Parameters:
Base64 string (with or without data URI prefix)
Returns:
bytes
Decoded image bytes
Raises:
If base64 data is invalid
Detect image format from bytes.
Uses magic bytes for fast detection, falls back to PIL for robust detection.
Parameters:
Image data as bytes
Returns:
data_designer.config.utils.image_helpers.ImageFormat
Detected ImageFormat
Raises:
If the image format cannot be determined
Check if a string is an image file path.
Parameters:
String to check
Returns:
bool
True if the string looks like an image file path, False otherwise
Check if a string is base64-encoded image data.
Parameters:
String to check
Returns:
bool
True if the string looks like base64-encoded image data, False otherwise
Check if a string is an image URL.
Parameters:
String to check
Returns:
bool
True if the string looks like an image URL, False otherwise
Load an image from a file path and return as base64.
Parameters:
Relative or absolute path to the image file.
Optional base path to resolve relative paths from.
Returns:
str | None
Base64-encoded image data or None if loading fails.
Download an image from a URL and return as base64.
Parameters:
HTTP(S) URL pointing to an image.
Request timeout in seconds.
Returns:
str
Base64-encoded image data.
Raises:
If the download fails with a non-2xx status.
Download an image from a URL asynchronously and return as base64.
Parameters:
HTTP(S) URL pointing to an image.
Request timeout in seconds.
Returns:
str
Base64-encoded image data.
Raises:
If the download fails with a non-2xx status.
Validate that an image file is readable and not corrupted.
Parameters:
Path to image file
Raises:
If image is corrupted or unreadable