API: Properly handle Images with RGBA color format (#5332)

This commit is contained in:
Ercan 2024-01-22 06:08:51 -05:00 committed by GitHub
parent db1da9f98d
commit 166fdf09f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -156,8 +156,9 @@ def convert_history(history):
img = Image.open(BytesIO(my_res.content))
except Exception:
raise 'Image cannot be loaded from the URL!'
buffered = BytesIO()
if img.mode in ("RGBA", "P"):
img = img.convert("RGB")
img.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
content = f'<img src="data:image/jpeg;base64,{img_str}">'