From d4248499ca594f1e79c187d3259e56526c643c55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 11 Apr 2024 15:19:41 +0200 Subject: [PATCH] Use white background to remove alpha channel of images Closes #13191 --- modules/product_image/product.py | 5 ++++- trytond/trytond/ir/avatar.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/product_image/product.py b/modules/product_image/product.py index 93800831ba..e06c6f8e76 100644 --- a/modules/product_image/product.py +++ b/modules/product_image/product.py @@ -170,7 +170,10 @@ class ImageMixin(_ImageMixin): img = PIL.Image.open(io.BytesIO(image)) img.thumbnail((SIZE_MAX, SIZE_MAX)) if img.mode in {'RGBA', 'P'}: - img = img.convert('RGB') + img.convert('RGBA') + background = PIL.Image.new('RGBA', img.size, (255, 255, 255)) + background.alpha_composite(img) + img = background.convert('RGB') img.save(data, format='jpeg', optimize=True, **_params) return data.getvalue() diff --git a/trytond/trytond/ir/avatar.py b/trytond/trytond/ir/avatar.py index 50e896b75c..3aab60c5be 100644 --- a/trytond/trytond/ir/avatar.py +++ b/trytond/trytond/ir/avatar.py @@ -129,7 +129,10 @@ class Avatar(ImageMixin, ResourceMixin, ModelSQL): if size > 2048: img = img.resize((2048, 2048)) if img.mode in {'RGBA', 'P'}: - img = img.convert('RGB') + img.convert('RGBA') + background = Image.new('RGBA', img.size, (255, 255, 255)) + background.alpha_composite(img) + img = background.convert('RGB') img.save(data, format='jpeg', optimize=True, **_params) return data.getvalue()