我有多个 PNG 图像文件,每个通道一个:红色、绿色、蓝色和黄色。
我如何将它们合并到一个 RBGA 图像中?
到目前为止我尝试了以下方法
from PIL import Image
red = Image.open('red.png')
green = Image.open('green.png')
blue = Image.open('blue.png')
yellow = Image.open('yellow.png')
rgb = Image.new('RGB', (blue.width, blue.height))
for im in [red, green, blue, yellow]:
rgb.paste(im, (0, 0))
rgb
Run Code Online (Sandbox Code Playgroud)
显然它不起作用,因为我只是覆盖了以前的图像。有任何想法吗?
更新:由于下面的评论,事实证明我可以将红色、绿色和蓝色文件合并为:
rgb = Image.merge("RGB",(red,green,blue))
Run Code Online (Sandbox Code Playgroud)
现在的问题是我如何使用黄色文件?
我已经完成了以下代码,但没有正确理解那里发生的事情。谁能解释一下如何在 Numpy 中填充颜色?
另外,我想以某种方式设置值,以1 to 0赋予光谱强度。例如-:0表示低强度,1表示高强度
import numpy as np
import matplotlib.pyplot as plt
a= np.zeros([256*6,256*6, 3], dtype=np.uint8) # init the array
# fill the array with rgb values to create the spectrum without the use of loops
#red
a[:,:,0] = np.concatenate(([255]*256, np.linspace(255,0,256), [0]*256, [0]*256, np.linspace(0,255,256), [255]*256))
#green
a[:,:,1] = np.concatenate((np.linspace(0,255,256), [255]*256, [255]*256, np.linspace(255,0,256), [0]*256,[0]*256))
#blue
a[:,:,2] = np.concatenate(([0]*256, [0]*256,np.linspace(0,255,256),[255]*256, [255]*256, np.linspace(255,0,256)))
plt.imshow(a) # this is different than what I am looking for
Run Code Online (Sandbox Code Playgroud)
预期输出-: