Reference
Here you can find the reference for the user facing API of Pylette. This consists of
the extract_colors
function, which is used to extract a color palette from an image,
and the Palette
and Color
classes, which are used to work with the extracted color palette.
Key Features
- JSON Export: Export palettes to structured JSON format with metadata
- Hex Colors: Access hex color codes through the
Color.hex
property - Semantic Fields: Export uses semantic field names (rgb, hsv, hls) instead of generic values
- Metadata: Rich metadata including extraction parameters, timing, and image info
- Batch Processing: Process multiple images with parallel execution support
Pylette.extract_colors
Extracts a set of 'palette_size' colors from the given image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
ImageInput
|
The input image. |
required |
palette_size
|
int
|
The number of colors to extract. |
5
|
resize
|
bool
|
Whether to resize the image before processing. |
True
|
mode
|
ExtractionMethod
|
The color quantization algorithm to use. |
KM
|
sort_mode
|
Literal['luminance', 'frequency'] | None
|
The mode to sort colors. |
None
|
alpha_mask_threshold
|
int | None
|
Optional integer between 0, 255. Any pixel with alpha less than this threshold will be discarded from calculations. |
None
|
Returns: Palette: A palette of the extracted colors.
Examples:
Colors can be extracted from a variety of sources, including local files, byte streams, URLs, and numpy arrays.
>>> extract_colors("path/to/image.jpg", palette_size=5, resize=True, mode="KM", sort_mode="luminance")
>>> extract_colors(b"image_bytes", palette_size=5, resize=True, mode="KM", sort_mode="luminance")
Source code in Pylette/src/color_extraction.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
|
Pylette.batch_extract_colors
Extract colors from multiple images in parallel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
progress_callback
|
Callable[[int, BatchResult], None] | None
|
Optional callback function called when each task completes. Receives (task_number, result) as arguments. |
None
|
Source code in Pylette/src/color_extraction.py
Pylette.Palette
Source code in Pylette/src/palette.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
extraction_params: ExtractionParams | None
property
Get the extraction parameters from metadata.
image_info: ImageInfo | None
property
Get the image information from metadata.
image_source: str | None
property
Get the image source from metadata.
processing_stats: ProcessingStats | None
property
Get the processing statistics from metadata.
source_type: SourceType | None
property
Get the source type from metadata.
__init__
Initializes a color palette with a list of Color objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
colors
|
list[Color]
|
A list of Color objects. |
required |
Source code in Pylette/src/palette.py
display
Displays the color palette as an image, with an option for saving the image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
w
|
int
|
Width of each color component. |
50
|
h
|
int
|
Height of each color component. |
50
|
save_to_file
|
bool
|
Whether to save the file or not. |
False
|
filename
|
str
|
Filename. |
'color_palette'
|
extension
|
str
|
File extension. |
'jpg'
|
Source code in Pylette/src/palette.py
export
Export palette to JSON format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str
|
File to save to (extension will be added automatically if not present). |
required |
colorspace
|
ColorSpace
|
Color space to use. |
RGB
|
include_metadata
|
bool
|
Whether to include metadata. |
True
|
Source code in Pylette/src/palette.py
random_color
Returns N random colors from the palette, either using the frequency of each color, or choosing uniformly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
N
|
int
|
Number of random colors to return. |
required |
mode
|
str
|
Mode to use for selection. Can be "frequency" or "uniform". |
'frequency'
|
Returns:
Type | Description |
---|---|
list[Color]
|
list[Color]: List of N random colors from the palette. |
Source code in Pylette/src/palette.py
save
Saves the color palette as an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
w
|
int
|
Width of each color component. |
50
|
h
|
int
|
Height of each color component. |
50
|
filename
|
str
|
Filename. |
'color_palette'
|
extension
|
str
|
File extension. |
'jpg'
|
Source code in Pylette/src/palette.py
to_json
Exports the palette to JSON format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str | None
|
File to save to. If None, returns the dictionary. |
None
|
colorspace
|
Literal[rgb, hsv, hls]
|
Color space to use. |
RGB
|
include_metadata
|
bool
|
Whether to include palette metadata. |
True
|
Returns:
Type | Description |
---|---|
dict[str, object] | None
|
dict | None: The palette data as a dictionary if filename is None. |
Source code in Pylette/src/palette.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
Pylette.Color
Source code in Pylette/src/color.py
hex: str
property
Returns the color as a hexadecimal string.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The color in hexadecimal format (e.g., "#FF5733"). |
hls: tuple[float, float, float]
property
Converts the RGB color to HLS color space.
Returns:
Type | Description |
---|---|
tuple[float, float, float]
|
tuple[float, float, float]: The color values in HLS color space. |
hsv: tuple[float, float, float]
property
Converts the RGB color to HSV color space.
Returns:
Type | Description |
---|---|
tuple[float, float, float]
|
tuple[float, float, float]: The color values in HSV color space. |
luminance: float
property
Calculates the luminance of the color.
Returns: float: The luminance of the color.
__init__
Initializes a Color object with RGBA values and frequency.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rgba
|
tuple[int, ...]
|
A tuple of RGBA values. |
required |
frequency
|
float
|
The frequency of the color. |
required |
Source code in Pylette/src/color.py
__lt__
Compares the frequency of this color with another color.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
Color
|
The other Color object to compare with. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the frequency of this color is less than the frequency of the other color, False otherwise. |
Source code in Pylette/src/color.py
display
Displays the color in a window of specified width and height.
Parameters: w (int): Width of the window in pixels. h (int): Height of the window in pixels.
Source code in Pylette/src/color.py
get_colors
Returns the color values in the specified color space.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
colorspace
|
ColorSpace
|
The color space to use. |
RGB
|
Returns:
Type | Description |
---|---|
tuple[int, ...] | tuple[float, ...]
|
tuple[int, ...] | tuple[float, ...]: The color values in the specified color space. |