Skip to content

Commit

Permalink
adds program that print dominant color
Browse files Browse the repository at this point in the history
  • Loading branch information
RajaVardhan-coder authored and Team-thedatatribune committed Oct 4, 2023
1 parent d9f8d7f commit c007f58
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions dataset/ImageData/code/Dominant-color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import cv2
import numpy as np
from collections import Counter


def extract_dominant_color(image_path):
# Read the image using OpenCV
image = cv2.imread(image_path)

# Convert the image from BGR to RGB format
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Reshape the image to a list of pixels
pixels = image.reshape(-1, 3)

# Use Counter to count the frequency of each color
color_counter = Counter(map(tuple, pixels))

# Get the most common color
most_common_color = color_counter.most_common(1)[0][0]

# Convert the most common color to hex format
hex_color = '#{:02X}{:02X}{:02X}'.format(*most_common_color)

return hex_color


def main():
image_path = '' # Replace with the path to your input image

dominant_color = extract_dominant_color(image_path)

print(f"Top Dominant Color: {dominant_color}")


if __name__ == "__main__":
main()

0 comments on commit c007f58

Please sign in to comment.