gpt4 book ai didi

python - 在 python 中屏蔽然后粘贴两个图像

转载 作者:行者123 更新时间:2023-11-30 22:30:40 24 4
gpt4 key购买 nike

我有一个看起来像这样的图像:

enter image description here

我想将背景转换为白色,将所有其他像素转换为黑色,以便我的图像看起来像这样:

enter image description here

假设原始图像是img,上面的结果是mask。当我尝试从原始图像中获取mask时,事情并没有按预期进行。我这样做了:

mask = np.ones_like(img)*255
mask[img > 0] = 0

理想情况下我应该得到预期的结果,但这就是我得到的结果。

enter image description here

此外,我还有另一张图像,如下所示:

enter image description here

我想将预期的蒙版粘贴到最终的日落图像上。如何使用 numpy/scipy/PIL/skimage 做到这一点?

最佳答案

由于我们希望将 img 中任何非黑色的内容在 mask 中设置为零,因此只需沿着三个 channel (最后一个轴)并使用该 bool 数组掩蔽到 mask -

mask[(img>0).any(-1)] = 0

给定样本 #1 的输出 -

enter image description here

将其与日落图像混合img2 -

from scipy.misc import imresize

mask_resized = imresize(mask, size=img2.shape)
out = (mask_resized==255)*img2

输出 -

enter image description here

关于python - 在 python 中屏蔽然后粘贴两个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45980728/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com