Python PIL: blending a transparent image with another

I need to blend an image over another image using the Pythons PIL Library.

As you can see in the image below, my two source images are A and B. When I do this:

imageA.paste(imageB, (0, 0), imageB) 

As a result, I get C, but the part at the top of the gray background is now transparent. Image D is what I get when I put B on top of A in Photoshop and what I need to achieve with PIL.

What am I doing wrong? How can I make B over A in PIL to get D instead of C?

Example Image

+6
source share
2 answers

I can not comment now (write restriction).

But I think that you really need to do this instead, according to your needs:

 imageB.paste(imageA, (0, 0), imageA) 

Basically, that is, make B the background image to get the desired results, because this is what I see in D.

EDIT: Looking around more, I found this: fooobar.com/questions/65355 / ...

I think you should definitely use the alpha_composite method, since the paste does not work properly for background images with transparency.

0
source

use RGBA for transparency mask

 imageA.paste(imageB, (0, 0), imageB.convert('RGBA')) 
0
source

Source: https://habr.com/ru/post/971691/


All Articles