I have two png images with transparent background
example:
and I'm trying to achieve something similar to the PIL paste()
function in pyvips, where I can merge two images and 'remove the transparent background' using a mask.
PIL example:
image = Image.open(filepath, 'r')
image_2 = Image.open(filepath, 'r')
image.paste(image_2, (50, 50), mask=mask)
Expected Result:
I've already tried to use the pyvips insert()
function, but both images retain their backgrounds.
Pyvips insert:
image = pyvips.Image.new_from_file(path, access='sequential')
image_2 = pyvips.Image.new_from_file(path, access='sequential')
image = image.insert(image_2, 50,50)
Pyvips Result:
How can I have the "expected result" with pyvips?
You can do it like this, using the composite()
function:
import pyvips
# Load background and foreground images
bg = pyvips.Image.new_from_file('WA8rm.png', access='sequential')
fg = pyvips.Image.new_from_file('Ny50t.png', access='sequential')
# Composite foreground over background and save result
bg.composite(fg, 'over').write_to_file('result.png')
If you want to add an offset in x and y, use:
bg.composite(fg, 'over', x=200, y=100).write_to_file('result.png')