我有两张透明背景的PNG图片

示例:

image_0

image_1

我正在try 实现一些类似于PIL paste()功能的功能,我可以合并两张图像,并使用蒙版来go 除透明背景.

PIL 示例:

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

Expected Result:

expected result

我已经try 使用pyvips insert()功能,但两个图像都保留了它们的背景.

pyramid 镶件:

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:

pyvips result

我怎么才能对绿脓疮有"预期的结果"呢?

推荐答案

您可以这样做,使用composite()函数:

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

enter image description here


如果要添加x和y的偏移量,请使用:

bg.composite(fg, 'over', x=200, y=100).write_to_file('result.png')

enter image description here

Python相关问答推荐

Pandas 除以一列中出现的每个值

剧作家Python没有得到回应

PywinAuto在Windows 11上引发了Memory错误,但在Windows 10上未引发

如果值不存在,列表理解返回列表

C#使用程序从Python中执行Exec文件

django禁止直接分配到多对多集合的前端.使用user.set()

对象的`__call__`方法的setattr在Python中不起作用'

为什么抓取的HTML与浏览器判断的元素不同?

如何在Polars中从列表中的所有 struct 中 Select 字段?

在两极中过滤

如果初始groupby找不到满足掩码条件的第一行,我如何更改groupby列,以找到它?

从旋转的DF查询非NaN值

Numpyro AR(1)均值切换模型抽样不一致性

Flask运行时无法在Python中打印到控制台

如何在一组行中找到循环?

如何使用Azure Function将xlsb转换为xlsx?

Django在一个不是ForeignKey的字段上加入'

当我定义一个继承的类时,我可以避免使用`metaclass=`吗?

如果列包含空值,则PANAS查询不起作用

.awk文件可以使用子进程执行吗?