gpt4 book ai didi

python - 叠加两个 numpy 数组,将第四平面视为 alpha 级别

转载 作者:太空狗 更新时间:2023-10-30 02:04:02 25 4
gpt4 key购买 nike

<分区>

我有两个形状为 (256, 256, 4) 的 numpy 数组。我想将第四个 256 x 256 平面视为 alpha 级别,并导出这些阵列已被覆盖的图像。

代码示例:

import numpy as np
from skimage import io

fg = np.ndarray((256, 256, 4), dtype=np.uint8)
one_plane = np.random.standard_normal((256, 256)) * 100 + 128
fg[:,:,0:3] = np.tile(one_plane, 3).reshape((256, 256, 3), order='F')
fg[:, :, 3] = np.zeros((256, 256), dtype=np.uint8)
fg[0:128, 0:128, 3] = np.ones((128, 128), dtype=np.uint8) * 255
fg[128:256, 128:256, 3] = np.ones((128, 128), dtype=np.uint8) * 128

bg = np.ndarray((256, 256, 4), dtype=np.uint8)
bg[:,:,0:3] = np.random.standard_normal((256, 256, 3)) * 100 + 128
bg[:, :, 3] = np.ones((256, 256), dtype=np.uint8) * 255

io.imsave('test_fg.png', fg)
io.imsave('test_bg.png', bg)

这会创建两个图像,fg:

test_fg.png

和背景:

test_bg :

我希望能够将 fg 叠加到 bg 上。也就是说,最终图像应该在左上角有灰色(因为 fg 的 alpha 为 1),在右下角混合了灰色和彩色噪声,在其他象限中有纯色噪声。我正在寻找类似 add 函数的东西,它可以给我一个新的 np 数组。

请注意,我不认为这与 this answer 相同,它使用 matplotlib.pyplot.plt 来覆盖图像和摆弄彩色 map 。我认为我不需要在这里摆弄彩色 map ,但也许答案是我需要。

我希望操作返回一个新的 np.array 的原因是因为我想用许多图像迭代地执行此操作,按顺序叠加。

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