From b0ff033d695ed7da2ae5c6c08735ed09da70324e Mon Sep 17 00:00:00 2001 From: Developer Team Date: Fri, 10 Apr 2015 06:50:37 -0500 Subject: [PATCH] [FIX] tools: add support for transparency of PNG images Fixes #2569 Closes #6260 --- openerp/tools/image.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/openerp/tools/image.py b/openerp/tools/image.py index 4473641b0f6..b64647dbb11 100644 --- a/openerp/tools/image.py +++ b/openerp/tools/image.py @@ -111,16 +111,16 @@ def image_save_for_web(image, fp=None, format=None): opt = dict(format=image.format or format) if image.format == 'PNG': opt.update(optimize=True) + alpha = False + if image.mode in ('RGBA', 'LA') or (image.mode == 'P' and 'transparency' in image.info): + alpha = image.convert('RGBA').split()[-1] if image.mode != 'P': # Get the alpha band alpha = image.split()[-1] # Floyd Steinberg dithering by default image = image.convert('RGBA').convert('P', palette=Image.WEB, colors=256) - # Set all pixel values below 128 to 255 and the rest to 0 - mask = Image.eval(alpha, lambda a: 255 if a <=128 else 0) - # Paste the color of index 255 and use alpha as a mask - image.paste(255, mask) - opt.update(transparency=255) + if alpha: + image.putalpha(alpha) elif image.format == 'JPEG': opt.update(optimize=True, quality=80) if fp: