I have a transparent png image foo.png
and I’ve opened another image with:
JavaScript
x
2
1
im = Image.open("foo2.png")
2
Now what I need is to merge foo.png
with foo2.png
.
(foo.png
contains some text and I want to print that text on foo2.png
)
Advertisement
Answer
JavaScript
1
8
1
from PIL import Image
2
3
background = Image.open("test1.png")
4
foreground = Image.open("test2.png")
5
6
background.paste(foreground, (0, 0), foreground)
7
background.show()
8
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.