akamhy / imagedominantcolor

Get the dominant color of any image
https://pypi.org/project/imagedominantcolor/
MIT License
5 stars 7 forks source link
dominant-color dominant-colors most-common-color most-dominant-color pypi-package python python-library python3

Get the dominant color of any image

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

Introduction

ImageDominantColor is a Python package/library for detecting dominant color of images.

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

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

What ImageDominantColor is not?

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

Installation

pip install imagedominantcolor -U
pip install git+https://github.com/akamhy/imagedominantcolor.git

Usage

>>> from imagedominantcolor import DominantColor
>>> file_path = "blue_butterfly.jpg" # Blue color is dominant
>>> dominantcolor = DominantColor(file_path)
>>> dominantcolor.dominant_color
'b'
>>> dominantcolor.rgb
(3, 6, 244)
>>> dir(dominantcolor)
['__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_color', 'dominant_color_of_pixel', 'dominant_color_of_pixels_of_image_array', 'g', 'generate_dominant_color_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_color_of_image', 'set_rgbl_value_of_image', 'total_pixels']
>>> dominantcolor.total_pixels
256
>>> dominantcolor.r
3
>>> dominantcolor.g
6
>>> dominantcolor.b
244
>>> dominantcolor.l
3
>>> dominantcolor.rgbl
(3, 6, 244, 3)
>>> repr(dominantcolor)
'DominantColor(r:3 g:6 b:244 l:3; dominant_color:b; resize_value:16; minimum_percent_difference_of_rgb:10)'
>>> str(dominantcolor)
'b'
>>>

Output dominant color and what their meanings are:

What are r, g, b and l attributes of DominantColor 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) 2022 Akash Mahanty.

Released under the MIT License. See license for details.