What happens to pixel mode when a photo is reduced to a small palette?

quantize maps colors into a limited palette and produces a P-mode image.

Pixel experiment and interpretation

Give three distinct gray samples two palette slots. At least two share an index. Approximation may suit a photograph, but semantic labels must retain their IDs.

from PIL import Image

def row(values):
    return Image.frombytes('L', (len(values), 1), bytes(values))

im=row([10,20,240]).convert('RGB').quantize(colors=2)
(im.mode,len(im.getcolors()))

Result ('P', 2)

中文

将照片限制为少量代表色时,像素模式如何变化?

quantize 将颜色映射到有限调色板,输出 P 模式图像。

像素实验与适用边界

三个不同的灰度样本只分配两个色槽,至少两个输入共用一个索引。用于照片压缩可以近似,类别掩膜则要保持语义编号。

from PIL import Image

def row(values):
    return Image.frombytes('L', (len(values), 1), bytes(values))

im=row([10,20,240]).convert('RGB').quantize(colors=2)
(im.mode,len(im.getcolors()))

结果 ('P', 2)

References / 参考来源

Keep reading / 继续阅读