hbllmutils.history.image

Image blob URL utilities.

This module provides helper functions for converting PIL.Image.Image objects to data: blob URLs that can be embedded directly in HTML or CSS. It focuses on safe in-memory conversion with base64 encoding and simple MIME type handling.

The module contains the following main components:

  • to_blob_url() - Convert a PIL image into a base64-encoded data URL

Example:

>>> from PIL import Image
>>> from hbllmutils.history.image import to_blob_url
>>> img = Image.new("RGB", (2, 2), color="red")
>>> url = to_blob_url(img, format="png")
>>> url.startswith("data:image/png;base64,")
True

Note

The returned blob URL string can be large for high-resolution images. Consider optimizing or resizing images before conversion for bandwidth efficiency.

to_blob_url

hbllmutils.history.image.to_blob_url(image: Image, format: str = 'jpg', **save_kwargs: Any) 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 an in-memory buffer in the specified format, then base64-encoded and wrapped in a data URL.

Parameters:
  • image (PIL.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 (Any) – Additional keyword arguments passed to PIL.Image.Image.save() (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.new("RGB", (1, 1), color="white")
>>> blob_url = to_blob_url(img, format="png")
>>> blob_url.startswith("data:image/png;base64,")
True
>>> # Use higher quality JPEG
>>> blob_url = to_blob_url(img, format="jpg", quality=95, optimize=True)