There's a bunch of questions here on SO which provide answers to the present question, however the output is not the expected.
The goal is to merge two RGBA images. The information on the alpha channel of each image is not the same.
The current (simplified) code is:
from PIL import Image
image = '1.png'
watermark = '2.png'
wmark = Image.open(watermark)
img = Image.open(image)
img.paste(wmark, (0, 0), wmark)
img.save("result.png", "PNG")
The two images are:
Background
Foreground
Expected output
Actual result
In case you don't see the difference, here are the alpha channels (inverted for better visualization) of the final versions.
Expected result - alpha channel
Actual result - alpha channel
So with that said, is there any way of doing this or am I doing something wrong?
EDIT - clarification following @zenpoy comment:
If the foreground image has a certain amount of opacity, I want that to be taken into account when superimposing both images, but I don't want the alpha channel of the second image to be added to the first. Much like putting a piece of glass (the foreground image) in front of a paper image (background).
In other words, if the background image was RGB instead of RGBA, the final image should have no alpha information.