ImagePalette
from blanket import ImagePalette provides Pillow-compatible palette objects,
including color allocation, copying, raw data, serialization, and the wedge,
negative, random, and sepia factories. load reads text palettes, GIMP
palettes, and GIMP RGB gradients. LUT generation, color indexing, palette ramps,
sepia generation, and gradient rendering use the native Rust backend. Python
preserves mutable palette storage and handles file parsing; random uses Python's
random generator to match Pillow's seeded behavior.
from blanket import ImagePalette
palette = ImagePalette.ImagePalette("RGBA")
red_index = palette.getcolor((255, 0, 0, 128))
mode, data = palette.getdata()
Reference
ImagePalette
ImagePalette(mode: str = 'RGB', palette: Sequence[int] | bytes | bytearray | None = None)
An interleaved color palette, with RGB entries by default.
Configure ImagePalette.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
Palette channel layout, normally |
'RGB'
|
palette
|
Sequence[int] | bytes | bytearray | None
|
Interleaved channel values, or None for an empty palette. |
None
|
Examples:
from blanket import ImagePalette
palette = ImagePalette.ImagePalette("RGB", [0, 0, 0, 255, 0, 0])
palette
property
writable
palette: Sequence[int] | bytes | bytearray
Mutable interleaved palette entries.
Examples:
from blanket import ImagePalette
palette = ImagePalette.wedge()
print(palette.palette)
colors
property
writable
colors: dict[tuple[int, ...], int]
Map channel tuples to their first palette index.
Examples:
from blanket import ImagePalette
palette = ImagePalette.wedge()
print(palette.colors)
copy
copy() -> ImagePalette
Copy the palette data and flags, rebuilding color lookup on demand.
Examples:
from blanket import ImagePalette
palette = ImagePalette.wedge()
result = palette.copy()
getdata
getdata() -> tuple[str, Sequence[int] | bytes | bytearray]
Return the raw mode and data, or the mode and serialized entries.
Examples:
from blanket import ImagePalette
palette = ImagePalette.wedge()
mode, entries = palette.getdata()
tobytes
tobytes() -> bytes
Serialize a non-raw palette to bytes.
Examples:
from blanket import ImagePalette
palette = ImagePalette.wedge()
entries = palette.tobytes()
getcolor
getcolor(color: tuple[int, ...], image: Image | _PaletteImage | None = None) -> int
Find or allocate a color, optionally reusing an unused image index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color
|
tuple[int, ...]
|
RGB or RGBA tuple matching the palette mode. |
required |
image
|
Image | _PaletteImage | None
|
Optional indexed image whose unused entries may be reused. |
None
|
Examples:
from blanket import ImagePalette
palette = ImagePalette.wedge()
palette = ImagePalette.ImagePalette("RGB")
index = palette.getcolor((255, 0, 0))
save
save(fp: str | IO[str]) -> None
Write a 256-entry text palette to a filename or text stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp
|
str | IO[str]
|
Filename or writable text stream. |
required |
Examples:
from blanket import ImagePalette
palette = ImagePalette.wedge()
from io import StringIO
output = StringIO()
palette.save(output)
raw
raw(rawmode: str, data: Sequence[int] | bytes | bytearray) -> ImagePalette
Wrap encoded palette data without interpreting its layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rawmode
|
str
|
Channel layout of the palette data, normally |
required |
data
|
Sequence[int] | bytes | bytearray
|
Raw encoded palette entries. |
required |
Examples:
from blanket import ImagePalette
result = ImagePalette.raw("RGB", bytes([0, 0, 0, 255, 0, 0]))
make_linear_lut
make_linear_lut(black: int, white: float) -> list[int]
Return a 256-entry linear intensity lookup table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
black
|
int
|
Black endpoint; only 0 is supported. |
required |
white
|
float
|
White endpoint of the linear ramp. |
required |
Examples:
from blanket import ImagePalette
result = ImagePalette.make_linear_lut(0, 255)
make_gamma_lut
make_gamma_lut(exp: float) -> list[int]
Return a 256-entry gamma correction lookup table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exp
|
float
|
Gamma exponent applied to normalized intensity values. |
required |
Examples:
from blanket import ImagePalette
result = ImagePalette.make_gamma_lut(2.2)
negative
negative(mode: str = 'RGB') -> ImagePalette
Create a descending grayscale palette.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
Palette channel layout, normally |
'RGB'
|
Examples:
from blanket import ImagePalette
result = ImagePalette.negative()
random
random(mode: str = 'RGB') -> ImagePalette
Create a palette with 256 random colors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
Palette channel layout, normally |
'RGB'
|
Examples:
from blanket import ImagePalette
result = ImagePalette.random()
sepia
sepia(white: str = '#fff0c0') -> ImagePalette
Create a black-to-sepia RGB palette.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
white
|
str
|
CSS color for the lightest palette entry. |
'#fff0c0'
|
Examples:
from blanket import ImagePalette
result = ImagePalette.sepia("#fff0c0")
wedge
wedge(mode: str = 'RGB') -> ImagePalette
Create an ascending grayscale palette.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
Palette channel layout, normally |
'RGB'
|
Examples:
from blanket import ImagePalette
result = ImagePalette.wedge()
load
load(filename: str) -> tuple[bytes, str]
Load a text palette, GIMP palette, or GIMP RGB gradient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to a text palette, GIMP palette, or GIMP gradient. |
required |
Examples:
from pathlib import Path
from tempfile import TemporaryDirectory
from blanket import ImagePalette
with TemporaryDirectory() as directory:
path = str(Path(directory) / "palette.txt")
ImagePalette.wedge().save(path)
entries, mode = ImagePalette.load(path)