我有一个numpy数组a的形状(512,512,4)
每个元素都是一个元组:(r,g,b,a)。它表示512x512 RGBA图像。在
我有一个numpy数组B的形状(512,512,3)
每个元素都是一个元组:(r,g,b)。它代表一个类似的RGB图像。在
我想把所有的a(alpha)值从a的每个元素快速复制到B中相应的元素中(基本上是传输alpha通道)。在
得到的B形将是(512,512,4)。在
我怎样才能做到这一点?该算法基于快速像素操作技术。在
代码:
在## . input_image is loaded using PIL/pillow
rgb_image = input_image
print(f"Image: {rgb_image}")
rgb_image_array = np.asarray(rgb_image) # convert to numpy array
print(f"Image Array Shape: {rgb_image_array.shape}")
gray_image = rgb_image.convert("L") # convert to grayscale
print(f"Gray image: {gray_image}")
gray_image_array = np.asarray(gray_image)
print(f"Gray image shape: {gray_image_array.shape}")
out_image_array = np.zeros(rgb_image_array.shape, rgb_image_array.dtype)
print(f"Gray image array shape: {out_image_array.shape}")
rows, cols, items = out_image_array.shape
# create lookup table for each gray value to new rgb value
LUT = []
for i in range(256):
color = gray_to_rgb(i / 256.0, positions, colors)
LUT.append(color)
LUT = np.array(LUT, dtype=np.uint8)
print(f"LUT shape: {LUT.shape}")
# get final output that uses lookup table technique.
# notice that at this point, we don't have the alpha channel
out_image_array = LUT[gray_image_array]
print(f"output image shape: {out_image_array.shape}")
# How do I get the alpha channel back from rgb_image_array into out_image_array
输出:
^{pr2}$