'Changing image color and then using alpha_composite adds artifacting on PNG images

I have two PNG images; one is a white circle, and the other is a square filled with white. What I am trying to do is open each image, change the color, then paste the circle (foreground) onto the background. Python code below:

from PIL import Image

background = Image.open("background.png").convert("RGBA")
rcolor = (0, 94, 35)
randDye = Image.new('RGBA', background.size, color=rcolor)
backgroundcomp = Image.composite(randDye, background, background)

foreground = Image.open("foreground.png").convert("RGBA")
rcolor = (54, 109, 252)
randDye = Image.new('RGBA', background.size, color=rcolor)
foregroundcomp = Image.composite(randDye, foreground, foreground)

output = Image.alpha_composite(backgroundcomp, foregroundcomp)
output.save("file.png")

Unfortunately this yields a really ugly "halo" around the newly changed circle image.

python image

This does not occur when layering the two images in Photoshop, adjusting them to the proper color via fill or hue/saturation.

photoshop image

You can try this yourself with background.png and foreground.png here:

background.png foreground.png

Should I be colorizing the above two images differently to prevent the odd artifacting I get? At first I thought combining the two images using alpha_composite was the fix; this does not appear to be the case after further testing.



Sources

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

Source: Stack Overflow

Solution Source