我有两张透明背景的png图像
示例:
我正在尝试实现类似于 pyvips 中的 PIL
paste()
函数的功能,我可以在其中合并两个图像并使用蒙版“删除透明背景”。
PIL 示例:
image = Image.open(filepath, 'r')
image_2 = Image.open(filepath, 'r')
image.paste(image_2, (50, 50), mask=mask)
预期结果:
我已经尝试使用 pyvips
insert()
功能,但两个图像都保留了其背景。
Pyvips 插入:
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 结果:
如何使用 pyvips 获得“预期结果”?
你可以这样做:
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')