Manuel
Manuel

Reputation: 205

Paste another image into an image with with Pillow (Python) respecting transparency

I have two 320x320 pixels png images and I need to paste one above the other with Python (the parts that are not colored are transparent):

enter image description here enter image description here

When I do it manually with GIMP (copy+paste), it gives me the desired output:

enter image description here

However, when pasting one above the other with Pillow I get an image of the red circle as if there was no transparency:

from PIL import Image

im1 = Image.open("square.png")
im2 = Image.open("circle.png")

im1.paste(im2)
im1

enter image description here

What am I doing wrong?

Upvotes: 0

Views: 731

Answers (1)

Bhavya Parikh
Bhavya Parikh

Reputation: 3400

from PIL import Image

im1 = Image.open("square.png")
im2 = Image.open("circle.png")

im2 paste on im1

Image.Image.paste(im1,im2,mask=im2)

im1.show()

here you can specify image as mask as im2

or you can use directly as

im1.paste(im2,mask=im2)

Upvotes: 3

Related Questions