Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PIL - Image Paste On Another Image With Alpha

I'm struggling a touch with pasting one image with a transparent background on another, also with a transparent background, with the right alpha/colour blending.

Here are some example images, red.png and blue.png:

red.pngblue.png

I want to paste blue.png on top of red.png, and achieve this effect:

Expected Result

That image was made by combining the two images in Photoshop, simply as two layers.

The closest I can get using the Python Imaging Library is:

Actual Result

with this code:

from PIL import Image

blue = Image.open("blue.png")
red = Image.open("red.png")
red.paste(blue, (0,0), blue)
red.save("result.png")

Do you see how the alpha and the colour is off where the two circles overlap? In the expected result image, the red and blue blends together in a purplish way, but there's an unwanted alpha halo in the actual result image.

How can I achieve my ideal result in PIL?

like image 391
DizzyDoo Avatar asked Oct 02 '22 14:10

DizzyDoo


1 Answers

The closest I got was to use the alpha_composite function found here. Works really rather well!

like image 56
DizzyDoo Avatar answered Oct 08 '22 01:10

DizzyDoo