added function to decode uploaded image data

This commit is contained in:
Brian Bjarke Jensen
2024-02-27 21:07:47 +01:00
parent 493592a293
commit 6018ee8f9f
+7
View File
@@ -1,6 +1,7 @@
from __future__ import annotations
import logging
from base64 import b64decode
from base64 import b64encode
from io import BytesIO
from pathlib import Path
@@ -122,6 +123,12 @@ class VisualCommunication(BaseModel):
image.load()
return VisualCommunication(name=name, image=image)
@classmethod
def decode_image(cls, content: str) -> Image.Image:
"""Decode image."""
_, content_data = content.split(',')
return Image.open(BytesIO(b64decode(content_data)))
@field_serializer('image')
def serialize_image(image: Image.Image) -> bytes: # type: ignore
buffer = BytesIO()