imgutils.data.blob
This module provides utilities for handling image blob URLs, including conversion between images and blob URLs, and validation of blob URL format.
The module supports:
Converting images to blob URLs with specified formats
Loading images from blob URLs
Validating image blob URL format
Handling various image formats and MIME types
to_blob_url
- imgutils.data.blob.to_blob_url(image: str | PathLike | bytes | bytearray | BinaryIO | Image, format: str = 'jpg', **save_kwargs) str [source]
Convert an image to a blob URL string.
- Parameters:
image (ImageTyping) – The input image, can be PIL Image, numpy array, or file path
format (str) – The desired image format for the blob URL, defaults to ‘jpg’
save_kwargs – Additional keyword arguments passed to PIL Image.save()
- Returns:
A blob URL string containing the encoded image data
- Return type:
str
- Example:
>>> img = Image.open('test.jpg') >>> blob_url = to_blob_url(img, format='png', quality=95) >>> print(blob_url) # data:image/png;base64,...</pre>
load_image_from_blob_url
- imgutils.data.blob.load_image_from_blob_url(blob_url: str) Image [source]
Load an image from a blob URL string.
- Parameters:
blob_url (str) – The blob URL string containing encoded image data
- Returns:
A PIL Image object
- Return type:
PIL.Image.Image
- Raises:
ValueError – If the blob URL uses an unsupported encoding method
- Warns UserWarning:
If MIME type doesn’t match the actual image format or is invalid
- Example:
>>> blob_url = "data:image/png;base64,..." >>> img = load_image_from_blob_url(blob_url) >>> img.show()
is_valid_image_blob_url
- imgutils.data.blob.is_valid_image_blob_url(blob_url: str) bool [source]
Efficiently validate the format of an image blob URL (without validating data content).
- Parameters:
blob_url (str) – The URL string to validate
- Returns:
True if the string is a valid image blob URL, False otherwise
- Return type:
bool
- Example:
Valid formats:
>>> is_valid_image_blob_url('data:image/png;base64,ABC') # True >>> is_valid_image_blob_url('data:image/svg+xml,<svg/>') # True >>> is_valid_image_blob_url('DATA:IMAGE/JPEG;quality=95,...') # True
Invalid formats:
>>> is_valid_image_blob_url('data:text/plain,hello') # False >>> is_valid_image_blob_url('data:image/png') # False >>> is_valid_image_blob_url('data:image/;base64,ABC') # False