Skip to main content

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

Project description

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 small dependency footprint: just NumPy and 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

principal_colors-0.2.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

principal_colors-0.2.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file principal_colors-0.2.0.tar.gz.

File metadata

  • Download URL: principal_colors-0.2.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for principal_colors-0.2.0.tar.gz
Algorithm Hash digest
SHA256 64d5b835cf688e13387c763c76fb009644171f9f24c8a95e707209990ec2985e
MD5 86b1d54cd6717396bbfbb881f7b6947a
BLAKE2b-256 2f79531b707df0696abbc40063accd11b5ed8294b84150bb5bb6732bc6e05d66

See more details on using hashes here.

Provenance

The following attestation bundles were made for principal_colors-0.2.0.tar.gz:

Publisher: publish-pypi.yml on nialljmiller/principal_colors

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file principal_colors-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for principal_colors-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e2bfff402bb99bdc02a65b11125c089c357bd3a6b1df42f59e0e06992149aa27
MD5 a5fe05d8209b4e82f0eb1abb1852c6ac
BLAKE2b-256 d57f0de3939d5ba95c0c67ad414f2c46d6775b9bc094d2557482573968dde8bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for principal_colors-0.2.0-py3-none-any.whl:

Publisher: publish-pypi.yml on nialljmiller/principal_colors

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page