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
|
Returns:
Name | Type | Description |
---|---|---|
Palette |
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
__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
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 RGB values and frequency.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rgb |
tuple[int, ...]
|
A tuple of RGB 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. |