Skip to content
Back to top
View this page

# `ImagePalette` module

The `ImagePalette` module contains a class of the same name to represent the color palette of palette mapped images.

Note

The `ImagePalette` class has several methods, but they are all marked as “experimental.” Read that as you will. The `[source]` link is there for a reason.

class PIL.ImagePalette.ImagePalette(mode: str = 'RGB', palette: Sequence[int] | bytes | bytearray | None = None)

  Color palette for palette mapped images

  Parameters:

    - mode – The mode to use for the palette. See: Modes. Defaults to “RGB”

    - palette – An optional palette. If given, it must be a bytearray, an array or a list of ints between 0-255. The list must consist of all channels for one color followed by the next color (e.g. RGBRGBRGB). Defaults to an empty palette.

  getcolor(color: tuple[int, ...], image: Image.Image | None = None) → int

    Given an rgb tuple, allocate palette entry.

    Warning

    This method is experimental.

  getdata() → tuple[str, Sequence[int] | bytes | bytearray]

    Get palette contents in format suitable for the low-level `im.putpalette` primitive.

    Warning

    This method is experimental.

  save(fp: str | IO[str]) → None

    Save palette to text file.

    Warning

    This method is experimental.

  tobytes() → bytes

    Convert palette to bytes.

    Warning

    This method is experimental.

  tostring() → bytes

    Convert palette to bytes.

    Warning

    This method is experimental.