How to merge a transparent png image with another image using PIL

from PIL import Image

background ="test1.png")
foreground ="test2.png")

background.paste(foreground, (0, 0), foreground)

First parameter to .paste() is the image to paste. Second are coordinates, and the secret sauce is the third parameter. It indicates a mask that will be used to paste the image. If you pass a image with transparency, then the alpha channel is used as mask.

Check the docs.

Leave a Comment