Set the correct mimetype in the base64 image

This commit is contained in:
cmdr2 2022-10-31 19:05:57 +05:30 committed by GitHub
parent 2f208832a9
commit 053bce7a8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -685,11 +685,13 @@ def img_to_base64_str(img, output_format="PNG"):
img.save(buffered, format=output_format) img.save(buffered, format=output_format)
buffered.seek(0) buffered.seek(0)
img_byte = buffered.getvalue() img_byte = buffered.getvalue()
img_str = "data:image/png;base64," + base64.b64encode(img_byte).decode() mime_type = "image/png" if output_format.lower() == "png" else "image/jpeg"
img_str = f"data:{mime_type};base64," + base64.b64encode(img_byte).decode()
return img_str return img_str
def base64_str_to_img(img_str): def base64_str_to_img(img_str):
img_str = img_str[len("data:image/png;base64,"):] mime_type = "image/png" if img_str.startswith("data:image/png;") else "image/jpeg"
img_str = img_str[len(f"data:{mime_type};base64,"):]
data = base64.b64decode(img_str) data = base64.b64decode(img_str)
buffered = BytesIO(data) buffered = BytesIO(data)
img = Image.open(buffered) img = Image.open(buffered)