im = im.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255)
mask = Image.eval(alpha, lambda a: 255 if a <= 128 else 0)
im.paste(255, mask)
im.save('logo_py.png', transparency=255)
Converting PNG32 to PNG8 with PIL while preserving transparency