[FIX] tools: add support for transparency of PNG images

Fixes #2569
Closes #6260
This commit is contained in:
Developer Team
2015-06-25 18:56:54 +02:00
committed by Nicolas Martinelli
parent 0275d8c273
commit b0ff033d69
+5 -5
View File
@@ -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: