We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

The ImagePalette Module

FIXME: This section doesn’t quite match the current implementation. For now, the safest way to attach a palette to an image is to use the putpalette method with the palette data in a string.

To use the ImagePalette class and related functions, import the ImagePalette module.


Attach a Palette to an Image (Sequence Syntax)
palette = []
for i in range(256):
    palette.extend((i, i, i)) # grayscale wedge

assert len(palette) == 768



Attach a Palette to an Image (Not Yet Supported)
import ImagePalette

palette = ImagePalette.ImagePalette("RGB")



Getting the Palette Contents Using Resize/Convert
assert im.mode == "P"

lut = im.resize((256, 1))
lut = lut.convert("RGB").getdata() 

# lut now contains a sequence of (r, g, b) tuples



ImagePalette.ImagePalette(mode=”RGB”) ⇒ palette instance

This constructor creates a new palette, mapping from “P” to the given mode. The palette is initialised to a linear grayscale ramp.