From c96f3d15c05733f577aaa5d4a48c105910e22a51 Mon Sep 17 00:00:00 2001 From: Emmanuel Jemeni Date: Mon, 3 Apr 2023 16:20:48 +0100 Subject: [PATCH] fix: Fixes bad transparency mask error --- ebook/image.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ebook/image.py b/ebook/image.py index 6bf4b07..8a50c10 100644 --- a/ebook/image.py +++ b/ebook/image.py @@ -63,7 +63,12 @@ def PIL_Image_to_bytes( return out_io.getvalue() elif image_format.lower() in ["jpeg", "jpg"]: - pil_image = pil_image.convert("RGB") + # Create a new image with a white background + background_img = Image.new('RGBA', pil_image.size, "white") + + # Paste the image on top of the background + background_img.paste(pil_image.convert("RGBA"), (0, 0), pil_image.convert("RGBA")) + pil_image = background_img.convert('RGB') pil_image.save(out_io, format=image_format, optimize=True, quality=95) return out_io.getvalue()