Python to Finding Dominant Color with Source Code

This script will take an image and it will find dominant color in it.

Prerequisites:

  • You only need to have installed opencv which is used for image preprocesssing.
  • Run the below script to install opencv
pip install opencv-python

Requirements:

  • opencv-python==4.3.0.36
  • numpy==1.19.1

Run the Script:

Run below command

python find-color.py
Code language: CSS (css)
  • Now Enter the path for image
  • Copy Your image path and enter in the command

Source Code:

color_finder.py

import cv2
import numpy as np

path = input("Enter Path :- ")
try:
    img = cv2.imread(path)
    cv2.imshow("img", img)
except Exception:
    print("Path not found")
    exit()


array = np.array(img)
unique, counts = np.unique(array, return_counts=True)

ocurrance = dict(zip(unique, counts))


a1_sorted_keys = sorted(ocurrance, key=ocurrance.get, reverse=True)
print(a1_sorted_keys[:3])


# Create a blank 300x300 black image
image = np.zeros((300, 300, 3), np.uint8)
# Fill image with red color(set each pixel to red)
image[:] = a1_sorted_keys[:3]


c = a1_sorted_keys[0]

# Create a blank 300x300 black image
color = np.zeros((300, 300, 3), np.uint8)
# Fill image with red color(set each pixel to red)
color[:] = (c, c, c)

print("Tone : " + str(a1_sorted_keys[:3]))
cv2.imshow("Tone", image)
print("color : " + str([c, c, c]))
cv2.imshow("color", color)

Output:

Leave a Comment