Cluster Colors (or other vectors)
Processing and clustering colors from images presents some challenges:
- Even a small (800x600) image will have up to 480,000 colors.
- Solutions like PIL's
Image.quantize, on the other hand, make the colors sample too coarse. - Even after reducing color variety, you're still dealing with 480,000 color instances.
- Solutions like
scikit-learn'sKMeanshandle some of these challenges, but are non-deterministic and not flexible in the ways that I'd like.
I provide three steps here:
Pool colors
Average similar colors. Specifically, this maps an 8-bit color space to an n-bit color space then averages colors in each bin. An argument, nbits, specifies the number of bits to use for each color channel. The default is 6, which reduces 17-million-ish possible colors to 300-thousand-ish possible colors. The downside is that the boundaries between n-bit bins are arbitrary. Heavy concentrations of near-identical colors will be split if a boundary passes through them.
Pooling colors from an image path will write a cache to your temp directory.
Cut colors
Reduce the number of colors by recursively splitting the color space along the longest axis. This is a median cut algorithm, but it's not constrained to x, y, or z axes. The longest axis is determined by the standard deviation of the colors in the cluster. I've made the cut just a little bit smarter than standard median cut, but this is essentially k-medoids without the re-distribution step, so it's more efficient, but not the best we can do. An argument, num, specifies the number of colors to reduce to. 512 is a good number, but if you're still missing some nuance, you can increase it.
Divisive and Agglomerative clustering
- Both are deterministic.
- Both handle frequency, weight, transparency.
- Both allow a user-defined proximity matrix, so you can use whatever delta function you like as long as
delta(a,a)is 0 anddelta(a,b)is never 0. Common choices are Euclidean, squared Euclidean, and delta-e. - Divisive uses a variation of median cut followed by a kmediods-like reassignment step to conversion.
- Agglomerative uses complete linkage.
- Divisive is more robust to outliers and will give more even-sized clusters.
- Divisive child clusters will not necessarily contain (or only contain) the members of the parent.
- Agglomerative is more likely to separate outliers.
- Agglomerative is heirarchical.
Divisive clustering is typically better for, "What are the five dominant colors in this image?"
Agglomerative clustering is typically better for, "How many colors do I need to represent this image with no more than delta==3 between any two cluster members?"
Installation
pip install cluster_colors
Basic usage
from cluster_colors import get_image_clusters
# find the five most dominant colors in an image
clusters = get_image_clusters(image_filename)
clusters.split_to_n(5)
exemplars = clusters.get_as_vectors()
# to save the cluster exemplars as an image file
show_clusters(split_clusters, "open_file_to_see_clusters")
Metadata
Release files for cluster-colors 0.17.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cluster_colors-0.17.1.tar.gz | 264.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cluster_colors-0.17.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 290.2 kB
Release files / cluster_colors-0.17.1.tar.gz
| Download URL | cluster_colors-0.17.1.tar.gz |
|---|---|
| Size | 264.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e2f96ffa7e03bee15111848fe238955118ba790b101047bcba9587895bcdfd8a
|
|
BLAKE2b-256 checksum How to use checksums |
3400b0de91cab36298de141c8285f23761172f872f15dc1f47ad50327d190b72
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|
Release files / cluster_colors-0.17.1-py3-none-any.whl
| Download URL | cluster_colors-0.17.1-py3-none-any.whl |
|---|---|
| Size | 25.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eb8ce2d49961ad9d346f789e73d14b5eb2b9ca9561d3b947255feffe948ff535
|
|
BLAKE2b-256 checksum How to use checksums |
867379a44c14775aa1f8bc666732ba2a72fb3c7c5b69518031e4e921110e56be
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|