如何将索引的彩色图像numpy数组转换为RGB颜色空间?

时间:2018-05-10 07:16:59

标签: python python-imaging-library

我想将索引的彩色图像数组转换为RGB色彩空间。我知道如果我有PIL读取图像,我可以实现这一点。

with Image.open(png_image_path) as img:
    rgb_image = np.asarray(img.convert('RGB'))

我想从numpy数组做同样的事情,而不是像PIL读取的图像对象一样。

image = Image.fromarray(indexed_image).convert('RGB')

但它并不像我想要的那样简单地工作。任何建议对我都有帮助!感谢您抽出宝贵时间。

1 个答案:

答案 0 :(得分:0)

你可以这样做:

scr = numpy.frombuffer(screen_buffer, dtype=numpy.uint8)
pal = numpy.frombuffer(palette_buffer, dtype=numpy.uint8).reshape((256, 3))

# Convert indexed color to RGB
arr = pal[scr]

data = arr.astype(numpy.uint8).tobytes()