| 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) |