PIL 的 Image.convert() 函数如何在“P”模式下工作

Vic*_*Xue 6 python image image-processing python-imaging-library

我有一组 24 位 png 文件,我想将它们转换为 8 位 png 文件。我使用了 PIL 的 Image.convert() 方法来解决这个问题。但是,在使用 mode 'P' 作为参数后,我发现具有相同 RGB 值的像素可以进行不同的转换。

我将一个示例图像传输到一个 numpy 数组中,原始 24 位 png 文件具有如下值:

RGB阵列

   ..., 
   [204, 102, 119],
   [204, 102, 119],
   [204, 102, 119],
   [204, 102, 119],
   [204, 102, 119],
   [204, 102, 119],
   [204, 102, 119],
   [204, 102, 119],
   [204, 102, 119],
   [204, 102, 119],
   [204, 102, 119], 
   ...
Run Code Online (Sandbox Code Playgroud)

使用模式'P'的转换函数后,图像值变成这样:

8 位数组

   ..., 98, 98, 134, 98, 98, 98, 134, 98, 98, 98, 134, ...
Run Code Online (Sandbox Code Playgroud)

代码示例:

from PIL import Image
import numpy as np
img = Image.open("path-to-file.png")
p_img = img.convert("P")
Run Code Online (Sandbox Code Playgroud)

我希望具有相同 RGB 值的像素以相同的方式转换。我知道像素被转换为调色板索引,但这对我来说仍然没有意义。我不熟悉 PIL 库。有人可以解释为什么会发生这种情况吗?提前致谢。


按照马克的例子实现了一些东西

import numpy as np
from PIL import Image
#from improc import GenerateNColourImage

# Set image height and width
N    = 6
h, w = 100, 100

# Generate repeatable random Numpy image with N^3 unique colours at most
n = np.random.randint(N, size=(h, w, 3), dtype=np.uint8)
# Work out indices of diagonal elements
diags = np.diag_indices(h)

# Intentionally set all diagonal elements same shade of blue
n[diags] = [10,20,200]
# Make Numpy image into PIL Image, palettise, convert back to Numpy array and check diagonals
a0 = Image.fromarray(n)

unique_colors = np.unique(n.reshape(-1, n.shape[2]), axis=0).shape
print(unique_colors)   #e.g. print (217, 3)
a1 = a0.convert('P')
a2 = np.array(a1)

# Look at diagonals - should all be the same
print(a2[diags])
print(' %s %d' % ("Number of unique colors:  ", np.unique(a2).shape[0]))
Run Code Online (Sandbox Code Playgroud)

打印的对角线像素值

... 154 154 154 154 154 154 124 154 160 160 160 154 160 ...
Run Code Online (Sandbox Code Playgroud)

“P”模式下的 8 位图像包含 125 个唯一的调色板索引。似乎无论如何 PIL 都会执行抖动。

Vas*_*o.S 2

当我们将图像转换为颜色模式时,这是正常行为P。调色板模式的工作方式是创建一个映射表,该表将索引(范围 0 - 255)与较大颜色空间(如RGB)中的离散颜色相对应。例如,图像中的 RGB 颜色值 (0, 0, 255)(纯蓝色)的索引为 1(只是一个假设的示例)。同样的过程会遍历原始图像中的每个唯一像素值(但在映射过程中,表大小不应超过 256)。因此,numpy 数组(或常规列表)具有如下值:-

   ..., 98, 98, 134, 98, 98, 98, 134, 98, 98, 98, 134, ...
Run Code Online (Sandbox Code Playgroud)

对应于映射表中的索引,而不是实际的颜色值本身。因此,您可以将它们解释为索引,在读取图像时将其转换为存储在该索引中的实际颜色值。

但这些像素值并不总是意味着图像是彩色模式的P。例如,如果您查看灰度图像 (L​​ ) 的像素数据,这些值看起来与调色板模式下的情况相同,但实际上对应于真实颜色值(或灰色阴影),而不是索引。