【发布时间】:2018-01-13 08:39:13
【问题描述】:
我有一个像 this one 这样的调色板图像和一个 numpy 数组中的二值化图像,例如像这样的正方形:
img = np.zeros((100,100), dtype=np.bool)
img[25:75,25:75] = 1
(当然真实的图像更复杂。)
我想做以下事情:
从调色板图像中提取所有 RGB 颜色。
对于每种颜色,以该颜色和透明背景保存
img的副本。
到目前为止,我的代码(见下文)可以将img 保存为具有透明背景的黑色对象。我正在努力的是一种提取 RGB 颜色的好方法,以便我可以将它们应用于图像。
# Create an MxNx4 array (RGBA)
img_rgba = np.zeros((img.shape[0], img.shape[1], 4), dtype=np.bool)
# Fill R, G and B with inverted copies of the image
# Note: This creates a black object; instead of this, I need the colors from the palette.
for c in range(3):
img_rgba[:,:,c] = ~img
# For alpha just use the image again (makes background transparent)
img_rgba[:,:,3] = img
# Save image
imsave('img.png', img_rgba)
【问题讨论】:
标签: python numpy image-processing scikit-image