Skip to main content

principal_colors

principal_colors is a dead-simple tool to extract a strong, distinct palette from any image.

It is designed around one obvious path for normal users:

principal-colors path/to/image.jpg

That prints the palette to stdout and also writes a single JSON file next to the image, such as:

image.palette.json

Under the hood, it uses OKLab distances and can optionally account for common color-vision deficiencies, but those are secondary features rather than the headline.

Why this rewrite

This package has been rewritten around a simpler public UX:

  • standard CLI flags instead of key=value
  • a positional image path
  • no import-time side effects
  • a clean Python API for both simple and detailed use
  • a single runtime dependency: Pillow
  • feature-rich advanced exports without cluttering the default experience

Installation

From PyPI:

pip install principal-colors

From a local checkout:

pip install .

For development:

python -m pip install -e '.[dev]'

Command line usage

The basic path is:

principal-colors image.jpg

That will:

  • print the palette as hex colors to stdout
  • save image.palette.json

Basic examples

principal-colors image.jpg
principal-colors image.jpg --colors 10
principal-colors image.jpg --stdout-only
principal-colors image.jpg --output my_palette.json

Advanced examples

principal-colors image.jpg --cb-mode all
principal-colors image.jpg --save-formats json,svg,png --output-prefix out/palette
principal-colors image.jpg --print-format rgb
principal-colors image.jpg --details

No arguments

If you run:

principal-colors

it shows an overview, a manual, and an ASCII logo.

Python API

The simple API returns hex colors by default:

from principal_colors import extract_palette

colors = extract_palette("image.jpg")
print(colors)

You can ask for other return types:

from principal_colors import extract_palette

rgb_colors = extract_palette("image.jpg", return_format="rgb")
result = extract_palette("image.jpg", return_format="result")

Or use the detailed API directly:

from principal_colors import extract_palette_result

result = extract_palette_result("image.jpg", num_colors=10)
print(result.hex_colors)
print(result.rgb_colors)
print(result.oklab_colors)
result.save()  # writes image.palette.json
result.save_many(["json", "svg", "png"], output_prefix="out/palette")

Public low-level helpers

The lower-level functions are intentionally public, so advanced users can do things like:

from principal_colors import rgb_to_oklab, oklab_to_rgb, simulate_cb

Output formats

The package can write:

  • JSON
  • TXT
  • CSV
  • SVG
  • PNG swatches
  • CSS variables
  • Tailwind JSON
  • Matplotlib style files
  • GIMP palette files

The default automatic save format is JSON because it is a reasonable, machine-readable single-file default.

API docs

A small API reference lives in docs/API.md.

Development

Run checks with:

ruff check .
pytest
python -m build
python -m twine check dist/*

License

MIT.

Metadata

Release files for principal-colors 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for principal-colors 0.3.0
File Size Uploaded
principal_colors-0.3.0.tar.gz 14.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for principal-colors 0.3.0
File Interpreter ABI Platform
principal_colors-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 28.0 kB

Release files / principal_colors-0.3.0.tar.gz

Download URL principal_colors-0.3.0.tar.gz
Size 14.6 kB
Tags Source
SHA-256 checksum
How to use checksums
941bbce0a87130995604ed48781310618fd4649755e75aea7b258c9df2919674
BLAKE2b-256 checksum
How to use checksums
59fbd3e0ef5b4b0576ca605e4d5e134b13852f3a5fb61f7c7ade4eb4f7621bb2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.

Transparency log

Release files / principal_colors-0.3.0-py3-none-any.whl

Download URL principal_colors-0.3.0-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e1b97262e1a9b9a32ba0782e5bec541342fcadf806618cf981f347f252647b4f
BLAKE2b-256 checksum
How to use checksums
4a1f388191d7e71282d0e5df4aba9e6fc7823a739b7e1a17c08d08770fc5779b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 2, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page