【问题标题】:How do you convert indexed color image numpy array to RGB color-space?如何将索引彩色图像 numpy 数组转换为 RGB 颜色空间?
【发布时间】:2018-05-10 07:16:58
【问题描述】:

我想将索引彩色图像数组转换为 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')

但它并不能简单地按照我想要的方式工作。任何建议都会对我有所帮助!感谢您抽出宝贵时间。

【问题讨论】:

  • 你能解释一下为什么你想以不同的方式做这件事吗?这在找出解决方法时可能会有所帮助。
  • 我已经为分段网络标记了数据,该数据被索引为类 arg 编号。在我训练好神经网络后,它会预测每个像素的索引号。所以现在我想可视化预测的像素。这就是为什么我想将索引颜色转换为 RGB 颜色空间。
  • @KousukeKuzuoka,你知道如何做到这一点吗?我还有一个来自 NN 的图像 IMAGE_1,其中包含编号从 1 到 10 的像素(因为我有 10 个类),我想使用我自己的调色板,看起来像这样 [[255 255 255 ], [168 10 25 ]...[222 10 45]] 并用它来转换图像 IMAGE_1使用调色板中的颜色转换为 RGB 图像。

标签: python python-imaging-library


【解决方案1】:

你可以这样做:

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()

【讨论】:

    猜你喜欢
    • 2011-07-10
    • 2018-04-09
    • 2019-07-21
    • 2014-11-28
    • 2019-01-01
    • 2018-08-26
    • 2020-05-04
    • 1970-01-01
    • 2021-03-01
    相关资源
    最近更新 更多