Building an Image Color Analyzer using Python | by Behic Guven | Jun, 2021


In this step, as you can understand from the title, we will be writing functions. I will define three functions that will be helpful for us. Functions are also an excellent method to simplify your programs.

Here are the functions with their definitions.


def rgb_to_hex(rgb_color):
hex_color = "#"
for i in rgb_color:
i = int(i)
hex_color += ("{:02x}".format(i))
return hex_color

In this function, we are converting an RGB color into Hex color format. This function will help at the end when visualizing the results of our analysis. Instead of having three different values (red, green, blue), we will have one output: hex value.


def prep_image(raw_img):
modified_img = cv2.resize(raw_img, (900, 600), interpolation = cv2.INTER_AREA)
modified_img = modified_img.reshape(modified_img.shape[0]*modified_img.shape[1], 3)
return modified_img

This function basically does the preprocessing of the image. If you want to make any changes to the picture before analyzing the colors, this is the function that you can use. We will resize and reshape the image in this step. Resizing is optional, but reshaping is needed for the color analysis model to work correctly. We will see it in the following function.


This is the function where the magic happens. I will cover in bullet points what is happening in the function.

def color_analysis(img):
clf = KMeans(n_clusters = 5)
color_labels = clf.fit_predict(img)
center_colors = clf.cluster_centers_
counts = Counter(color_labels)
ordered_colors = [center_colors[i] for i in counts.keys()]
hex_colors = [rgb_to_hex(ordered_colors[i]) for i in counts.keys()]
plt.figure(figsize = (12, 8))
plt.pie(counts.values(), color_labels = hex_colors, colors = hex_colors)
  • First, we are using k-Means to cluster the top colors. Inside the function we are passing the value of how many clusters do we want to divide. Here is the documentation for K-Means clustering. After clustering we predict the colors that weigh the most — meaning getting the most area on the image.
  • Secondly, we are calling the Counter function. Counter creates a container to the elements as dictionary keys, and their volume is store as dictionary values. If you are not familiar with dictionaries, they store data in key: value pairs. They are like function, and when you pass in the “key,” you can “value” as a return. Then we are ordering the colors according to the keys.
  • Thirdly, we are passing those colors in the rgb_to_hex function so that we can get the hex values of the colors.
  • And lastly, the visualization of the result. I decided to go with a pie chart, which will be helpful to understand the weight of each color in the whole picture. After plotting the figure, I am also saving it into the computer using the savefig method. This way, we have a record of the result.

Before we move to the final step, I would like to share a compelling article related to our computer vision project: Object Detection via Color-based Image Segmentation using Python by Salma Ghoneim.

Source link


Please enter your comment!
Please enter your name here