[Python] How does a Palette work in Python Imaging Library?

Discussão em 'Python' iniciado por Stack, Março 19, 2025.

  1. Stack

    Stack Membro Participativo

    Suppose I have an RGB24 image in which the color of each pixel evenly encodes a (raw) data byte.

    Scheme:
    Channel red encodes Bit 7:6
    (R: 0-63 = 0b00, 64-127 = 0b01, 128-191 = 0b10, 192-255 = 0b11)

    Channel green encodes Bit 5:3
    (G: 0-31 = 0b000, 32-63 = 0b001, 64-95 = 0b010, 96-127 = 0b011, ...)

    Channel blue encodes Bit 2:0
    (B: 0-31 = 0b000, 32-63 = 0b001, 64-95 = 0b010, 96-127 = 0b011, ...)

    Example:
    RGB = [150, 40, 94] represents (raw) data byte 0b10001010.

    Question:
    I would like to use the Python Imaging Library (PIL) to decode my image. If I got it right, then I would need to define a palette and apply it to my image using the quantize() method (as described here). However, I don't understand how the mapping of the colors does work. How does the palette translate the original 24 Bits of color information into 8 Bits?

    Finally: How would I need to set up my palette so that the decoding works according to my scheme?

    Continue reading...

Compartilhe esta Página