File size: 675 Bytes
fa8f4e5 fbd31cc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
from PIL import Image
def convert_png_with_alpha_to_png(png_path, output_png_path, background_color=(255, 255, 255)):
png = Image.open(png_path)
mode = png.mode
png.load()
background = Image.new("RGB", png.size, background_color)
if 'A' in mode:
background.paste(png, mask=png.split()[3])
else:
background.paste(png)
background.save(output_png_path, 'PNG')
from PIL import Image
png = Image.open(object.logo.path)
png.load() # required for png.split()
background = Image.new("RGB", png.size, (255, 255, 255))
background.paste(png, mask=png.split()[3]) # 3 is the alpha channel
background.save('foo.jpg', 'JPEG', quality=80) |