Skip to main content

Perceptually sorted colors

Project description

The colorsort python library provides algorithms for sorting colors.

You can use colorsort to:

  • Convert an image into a color vector
  • Reduce the number of colors in a color vector while maintaining the overall pallete
  • Sort the colors in a color vector

For an interactive demo check out this jupyter notebook.

Example converting image into color vector:

>>> import colorsort as csort
>>> num_x_pixels = 16
>>> num_y_pixels = 16
>>> num_channels = 3
>>> image_shape = (num_x_pixels, num_y_pixels, num_channels)
>>> image_rgb = np.random.randint(0,255,image_shape).astype(np.uint8)
>>> vec_rgb = csort.image_to_vec(image_rgb)

Casting the array to uint8 is crucial. Colorsort uses opencv under the hood and expects uint8.

Example reducing the number of colors in a color vector

>>> print(vec_rgb.shape)
(256, 3)
>>> vec_rgb_reduced = csort.reduce_colors(vec_rgb, 10)
>>> print(vec_rgb_reduced.shape)
(10, 3)

Colors are reduced using K-means clustering in the LAB colorspace. If the input has fewer than 10 colors, a copy of the input color vector is returned.

Example sorting colors in a color vector

>>> vec_rgb_sorted = csort.sort_colors(vec_rgb_reduced)
>>> print(vec_rgb_sorted.shape)
(10, 3)

The color sorting algorighm maps the problem of sorting colors onto a travelling salesman problem. The route starts at the brightest color, and ends at the darkest color. The distance between two colors is calculated using the Delta E CIE2000 standard. By finding the shortest path that visits all colors the resulting colors are perceptually sorted, with minimal jumps between neighboring colors. This is useful for constructing colormaps from arbitrary images.

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

colorsort-0.0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

colorsort-0.0.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file colorsort-0.0.1.tar.gz.

File metadata

  • Download URL: colorsort-0.0.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.11

File hashes

Hashes for colorsort-0.0.1.tar.gz
Algorithm Hash digest
SHA256 91c0024543549c8e781666342b4cae580b0a04de898c55750cafc7b18de17ec9
MD5 2e0bfd5d9be8e2ce5a6ed5a88444d28c
BLAKE2b-256 91bbc2fdba9152164e7851216df69cc481341930391a9fbfadf2034e4aa45ae3

See more details on using hashes here.

File details

Details for the file colorsort-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: colorsort-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.11

File hashes

Hashes for colorsort-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f718e08cb4de03c5f34ef69298f8470f985fa3a789d3a431cdbde4355e8809b4
MD5 1c8e69c2bf6eb774ca99ed8f4defe021
BLAKE2b-256 d57407ac3ae16f955e125c4fb344aca68acc5e2fe47fd969c12e6f727899ffa6

See more details on using hashes here.

Supported by

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