-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds program that print dominant color
- Loading branch information
1 parent
d9f8d7f
commit c007f58
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |