调色板-单波段图像显示

from PIL import Image
import numpy as np


colors = [(255, 255, 255),(0, 255, 0), (255, 0, 0), (0, 0, 255),(255, 255, 0), (255, 0, 255), (0, 255, 255), (0, 0, 0)]
              

# 创建一个空白的调色板(256个RGB值)
palette = [0] * 256 * 3

# 将颜色列表填充到调色板中(从索引1开始,索引为零表示背景)
for i in range(len(colors)):
    palette[i * 3: i * 3 + 3] = colors[i]

palette_bytes = bytes(palette) # 将调色板转换为字节类型1

arr = np.zeros((100, 800))
irr = np.r_[arr, arr+1, arr+2, arr+3, arr+4, arr+5, arr+6, arr+7]
img = Image.fromarray(irr).convert("P")
img.putpalette(palette_bytes)
img.show()

# img.save("*.png")  # 保存 最好png格式 其它格式可能报错 未详细研究

 

posted @ 2023-03-16 11:59  ddzhen  阅读(19)  评论(0编辑  收藏  举报