Skip to content
Prev Previous commit
Next Next commit
Created utils for working with URL-encoded images
  • Loading branch information
jamesbraza committed Oct 9, 2025
commit eb96e207fe6a01feab9e44953fa8bdfa193cb577
14 changes: 14 additions & 0 deletions packages/lmi/src/lmi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,17 @@ def validate_image(path: "StrOrBytesPath | IO[bytes]") -> None:

with Image.open(path) as img:
img.load()


def encode_image_as_url(image_type: str, image_data: bytes | str) -> str:
"""Convert image data to an RFC 2397 data URL format."""
if isinstance(image_data, bytes):
image_data = bytes_to_string(image_data)
return f"data:image/{image_type};base64,{image_data}"


def is_encoded_image(image: str) -> bool:
"""Check if the given image is a GCS URL or a RFC 2397 data URL."""
return image.startswith("gs://") or (
image.startswith("data:image/") and ";base64," in image
)