【问题标题】:Python: How to apply WEB palette after Image.fromarray() using Pillow?Python:如何使用 Pillow 在 Image.fromarray() 之后应用 WEB 调色板?
【发布时间】:2017-02-02 10:40:46
【问题描述】:

Pillow 可以将字节数组转换为图像:

img = Image.fromarray((data).astype('uint8'), 'P')

但它将是灰度图像。 我知道 PIL 有一个img.putpalette(some_palette) 方法

如何让 WEB 调色板将其应用于图像?

【问题讨论】:

    标签: python image python-3.x pillow color-palette


    【解决方案1】:

    我不确定您所说的 WEB 调色板是什么意思,但是
    枕头中的调色板由包含一系列 rgb 值的数组定义。 例如

    im.putpalette([
      0, 0, 0, # black background
      255, 0, 0, # index 1 is red
      255, 255, 0, # index 2 is yellow
      255, 153, 0, # index 3 is orange
    ])
    

    因此,您可能希望查找调色板的 rgb 值,并在代码中执行此操作。

    im = Image.fromarray((data).astype('uint8'), 'P')
    im.putpalette(some_palette)
    

    reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-21
      • 2015-12-28
      • 2019-03-05
      • 1970-01-01
      • 2022-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多