refactor: share backend image serialization helpers

This commit is contained in:
Dariusz L
2026-08-13 05:39:24 +02:00
parent f60b7c0d8d
commit 212d6b7728
5 changed files with 170 additions and 55 deletions
+7 -1
View File
@@ -29,4 +29,10 @@ def file_to_data_url(path: str, *, mime_type: str = "image/png") -> str:
return _bytes_to_data_url(image_file.read(), mime_type)
__all__ = ["file_to_data_url", "pil_to_data_url"]
def data_url_to_pil(data_url: str) -> Image.Image:
"""Decode an image data URL into a lazily opened PIL image."""
image_bytes = base64.b64decode(data_url.split(",")[1])
return Image.open(io.BytesIO(image_bytes))
__all__ = ["data_url_to_pil", "file_to_data_url", "pil_to_data_url"]