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 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.

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.3.0.tar.gz (14.6 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.3.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for principal_colors-0.3.0.tar.gz
Algorithm Hash digest
SHA256 941bbce0a87130995604ed48781310618fd4649755e75aea7b258c9df2919674
MD5 dddd229717b1e32a377b3e796430b67c
BLAKE2b-256 59fbd3e0ef5b4b0576ca605e4d5e134b13852f3a5fb61f7c7ade4eb4f7621bb2

See more details on using hashes here.

Provenance

The following attestation bundles were made for principal_colors-0.3.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.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for principal_colors-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e1b97262e1a9b9a32ba0782e5bec541342fcadf806618cf981f347f252647b4f
MD5 b8374d83c4239f5a1f328a3194ec994c
BLAKE2b-256 4a1f388191d7e71282d0e5df4aba9e6fc7823a739b7e1a17c08d08770fc5779b

See more details on using hashes here.

Provenance

The following attestation bundles were made for principal_colors-0.3.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