With the Python Imaging Library (PIL), how does one compose an image with an alpha channel over another image?

This appears to do the trick:

from PIL import Image
bottom = Image.open("a.png")
top = Image.open("b.png")

r, g, b, a = top.split()
top = Image.merge("RGB", (r, g, b))
mask = Image.merge("L", (a,))
bottom.paste(top, (0, 0), mask)
bottom.save("over.png")

Leave a Comment