'Pasting image with transparency leaves artifacts

I have some code like such:

background = Image.open("background.png").convert("RGBA")
foreground = Image.open("foreground.png").convert("RGBA")
background.paste(foreground, (0, 0), foreground)

For whatever reason, this adds some artifacting that I do not see in Photoshop when stacking the layers:

pillow paste output

See the equivalent region/layer stacked in Photoshop here for comparison:

photoshop layers

Any idea why this is? Should I be loading my png file differently, or saving it with max quality somehow?



Solution 1:[1]

Issue resolved with:

Image.alpha_composite(background, foreground).save("output.png", format="png")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Not A SWE