refactor: centralize image loading and serialization

This commit is contained in:
Dariusz L
2026-08-13 04:26:51 +02:00
parent 5f660629ec
commit 66faa3135a
13 changed files with 169 additions and 209 deletions
+32
View File
@@ -0,0 +1,32 @@
"""Shared image serialization helpers for LayerForge backend boundaries."""
import base64
import io
from PIL import Image
def _bytes_to_data_url(data: bytes, mime_type: str) -> str:
encoded = base64.b64encode(data).decode("ascii")
return f"data:{mime_type};base64,{encoded}"
def pil_to_data_url(
image: Image.Image,
*,
image_format: str = "PNG",
mime_type: str = "image/png",
) -> str:
"""Serialize a PIL image using the existing PNG data-URL contract."""
buffer = io.BytesIO()
image.save(buffer, format=image_format)
return _bytes_to_data_url(buffer.getvalue(), mime_type)
def file_to_data_url(path: str, *, mime_type: str = "image/png") -> str:
"""Read an image file without transforming its bytes into a data URL."""
with open(path, "rb") as image_file:
return _bytes_to_data_url(image_file.read(), mime_type)
__all__ = ["file_to_data_url", "pil_to_data_url"]