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.
Pylette.extract_colors
Extracts a set of 'palette_size' colors from the given image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
ImageType_T
|
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
|
Literal['KM'] | Literal['MC']
|
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
Pylette.Palette
Source code in Pylette/src/palette.py
9 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 |
|
__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
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 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_csv
Dumps the palette to stdout. Saves to file if filename is specified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str | None
|
File to dump to. |
None
|
frequency
|
bool
|
Whether to dump the corresponding frequency of each color. |
True
|
colorspace
|
Literal['rgb', 'hsv', 'hls']
|
Color space to use. |
'rgb'
|
stdout
|
bool
|
Whether to dump to stdout. |
True
|
Source code in Pylette/src/palette.py
Pylette.Color
Source code in Pylette/src/color.py
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
|
Literal['rgb', 'hsv', 'hls']
|
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. |