Skip to content

Extract dominant colors from images using K-means

License

Notifications You must be signed in to change notification settings

despawnerer/palletize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

palletize

PyPI version

palletize extracts dominant colors from images using K-means algorithm. Meant to be used as a library, CLI script is included for ease of testing.

It uses pillow, numpy and scipy, so it's slow to initialize, but pretty snappy afterwards. K-means is a bit random, so expect results for the same image to vary slightly from time to time.

Installation

$ pip install palletize

Usage

Library

from palletize import extract_dominant_colors

extract_dominant_colors(file_or_filename, count=3, clusters=None, iterations=30, resize_to=300)

Return a list of count dominant colors from the image as 3-tuples (r, g, b).

  • count is the number of colors you want to get;
  • clusters is the number of color groups to build from the image before getting count of dominant colors, by default it's the same as count if you're extracting more than one color, or 3 if count is 1;
  • iterations is the number of iterations for the algorithm to go through before settling: the higher it is, the better and more stable are the results, but the longer it takes;
  • resize_to is the pixel size of the larger side of the image to downscale it to speed up processing: pass None to disable downscaling.

CLI

palletize filename [filename ...]

About

Extract dominant colors from images using K-means

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages