hbllmutils.history.image

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

hbllmutils.history.image.to_blob_url(image: Image, format: str = 'jpg', **save_kwargs) str[source]

Convert a PIL Image to a blob URL string.

This function encodes an image into a base64 data URL that can be embedded directly in HTML or CSS. The image is saved to a buffer in the specified format, then base64-encoded and wrapped in a data URL.

Parameters:
  • image (Image.Image) – The PIL Image object to convert

  • format (str) – The desired image format for the blob URL (e.g., ‘jpg’, ‘png’, ‘webp’), defaults to ‘jpg’

  • save_kwargs (dict) – Additional keyword arguments passed to PIL Image.save() method (e.g., quality, optimize)

Returns:

A blob URL string in the format ‘data:{mime_type};base64,{encoded_data}’

Return type:

str

Example::
>>> from PIL import Image
>>> img = Image.open('test.jpg')
>>> blob_url = to_blob_url(img, format='png', quality=95)
>>> print(blob_url[:50])  # data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...
>>> # Use with higher quality JPEG
>>> blob_url = to_blob_url(img, format='jpg', quality=95, optimize=True)