back next

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.

Examples #

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

im.putpalette(palette)

 

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

palette = ImagePalette.ImagePalette("RGB")
palette.putdata(...)

im.putpalette(palette)

 

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

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

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

Classes #

ImagePalette #

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.

 

A Django site. rendered by a django application. hosted by webfaction.