from PIL import Image def flatten(img, bgcolor): # Replace transparency with bgcolor if img.mode in ("RGB"): return img return Image.alpha_composite(Image.new("RGBA", img.size, bgcolor), img).convert("RGB")