如何在PYVIPS中将没有背景的PNG插入到另一个PNG?

问题描述 投票:0回答:1

我有两张透明背景的png图像

示例:

image_0

image_1

我正在尝试实现类似于 pyvips 中的 PIL

paste()
函数的功能,我可以在其中合并两个图像并使用蒙版“删除透明背景”。

PIL 示例:

image = Image.open(filepath, 'r')
image_2 = Image.open(filepath, 'r')
image.paste(image_2, (50, 50), mask=mask)

预期结果:

expected result

我已经尝试使用 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 result

如何使用 pyvips 获得“预期结果”?

python python-imaging-library libvips
1个回答
0
投票

你可以这样做:

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')

© www.soinside.com 2019 - 2024. All rights reserved.