Skip to main content

Get the dominant colour of any image

Build Status codecov pypi Downloads PyPI - Python Version Code style: black

Introduction

ImageDominantColour is a Python package/library for detecting dominant colour of images.

It can take any input image and tell the dominant colour of the image. It doesn't use k-means clustering for detecting dominant colour but instead quantizes the individual pixels and calculates the statistical mode of the quantized values.

ImageDominantColour does not depend on numpy unlike most of the other implementations for the same task and is also fast and minimalist.

What ImageDominantColour is not?

ImageDominantColour does not calculates the average colour of the image. Also note that the average colour of an image is not same as its dominant colour.

Installation

pip install imagedominantcolour -U
  • Install directly from GitHub:
pip install git+https://github.com/demmenie/imagedominantcolour.git

Usage

>>> from imagedominantcolour import DominantColour
>>> file_path = "blue_butterfly.jpg" # Blue colour is dominant
>>> dominantcolour = DominantColour(file_path)
>>> dominantcolour.dominant_colour
'b'
>>> dominantcolour.rgb
(3, 6, 244)
>>> dir(dominantcolour)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'b', 'counter', 'dominant_colour', 'dominant_colour_of_pixel', 'dominant_colour_of_pixels_of_image_array', 'g', 'generate_dominant_colour_of_pixels_of_image_array', 'image', 'image_data', 'image_path', 'l', 'minimum_percent_difference_of_rgb', 'mpd', 'r', 'resize_value', 'resized_image', 'rgb', 'rgbl', 'set_dominat_colour_of_image', 'set_rgbl_value_of_image', 'total_pixels']
>>> dominantcolour.total_pixels
256
>>> dominantcolour.r
3
>>> dominantcolour.g
6
>>> dominantcolour.b
244
>>> dominantcolour.l
3
>>> dominantcolour.rgbl
(3, 6, 244, 3)
>>> repr(dominantcolour)
'DominantColour(r:3 g:6 b:244 l:3; dominant_colour:b; resize_value:16; minimum_percent_difference_of_rgb:10)'
>>> str(dominantcolour)
'b'
>>>

Output dominant colour and what their meanings are:

  • r - Red is the dominant colour in the image.
  • g - Green is the dominant colour for the image.
  • b - Blue is the dominant colour.
  • l - It is lowercase L and it implies that the image is a mostly a grayscale image. L for luminance and most of the image lacks colour.
  • n - None of the colour out of r, g and b are dominant but the image is not grayscale. It implies that the image has equal regions where 2 or 3 colours dominate, example here. You can check the rgb attribute to decide what to do with such cases.

What are r, g, b and l attributes of DominantColour objects?

The library shrinks the image before checking the dominant color and the default resize value is 256. Thus every image is shrunk to a 256 pixels image. The r,g,b and l attributes indicate the number of pixels which have r,g,b and l as dominating value.

License

License: MIT

Copyright (c) 2024 Akash Mahanty.

Forked by Chico Demmenie.

Released under the MIT License. See license for details.

Metadata

Release files for imagedominantcolour 1.0.2

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

Source distribution (sdist)

Source distribution for imagedominantcolour 1.0.2
File Size Uploaded
imagedominantcolour-1.0.2.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for imagedominantcolour 1.0.2
File Interpreter ABI Platform
imagedominantcolour-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / imagedominantcolour-1.0.2.tar.gz

Download URL imagedominantcolour-1.0.2.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f62c255a7143bd76070f9bc8492c3339fb65e0b4749059ef38edd8c2660e9028
BLAKE2b-256 checksum
How to use checksums
a684dad94c0f8a1096a0192fd3c586029a833cf45893f5d791ed9e7541e95309
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.2

Release files / imagedominantcolour-1.0.2-py3-none-any.whl

Download URL imagedominantcolour-1.0.2-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4c74c7fd8e88d6efc83feb9b431d565c62d4ac39b9d139a2d792f5cf2dca752b
BLAKE2b-256 checksum
How to use checksums
ccc7b50e59547254f4196ca6bd74e234dc56551394232f08a261435cb45076ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.2

Release history Release notifications | RSS feed

This release

1.0.2 This release

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