Serialize PIL Image in its format (#2201)

This commit is contained in:
Masen Furer 2023-11-20 16:17:09 -08:00 committed by GitHub
parent 5198daebae
commit 5eb4c4e62a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -95,10 +95,11 @@ try:
The serialized image.
"""
buff = io.BytesIO()
image.save(buff, format="PNG")
image.save(buff, format=getattr(image, "format", None) or "PNG")
image_bytes = buff.getvalue()
base64_image = base64.b64encode(image_bytes).decode("utf-8")
return f"data:image/png;base64,{base64_image}"
mime_type = getattr(image, "get_format_mimetype", lambda: "image/png")()
return f"data:{mime_type};base64,{base64_image}"
except ImportError:
pass