0
我有一個像this one調色板圖像,並在numpy的陣列的二值化圖像,例如一個正方形像這樣:獲得從調色板圖像RGB顏色和適用於二值圖像
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)