Monday 9 November 2020

Increasing the range of colors detected in an image

Helloo,

I am creating a project where I am trying to extract as output the number of colors in an image. I have been able to get the number of colors and their names along with their count but the problem is that it goes through every pixel which is getting different degrees of all the colors even if it is white and black.

So, here is my question how do I extend the degrees so that it shows only white and not the different grades of white and is there a better way to extract the number of colors in an image with their count and percentage?

To summarize: The output is getting too many colors for an image that is 2 colors only (by eyesight), I want to get an outcome that close the basic visible colors only.

I also have included only the colors that are greater than 1%

colors_dict = {..."FE0000": "Red","C71585": "Red-violet"} <------------ huge dictionary of colors this is just one example

img = Image.open("Moustiques_Design_2.JPG")
size = w, h = img.size
data = img.load()

colors = []
for x in range(w):
    for y in range(h):
        color = data[x, y]
        hex_color_lower = ''.join([hex(c)[2:].rjust(2, '0') for c in color])
        hex_color = hex_color_lower.upper()
        colors.append(hex_color)

total = w * h

color_hex = []
color_count = []
color_percent = []

df = pd.DataFrame()
for color, count in Counter(colors).items():
    percent = count/total * \
        100  # Do not make it int. Majority of colors are < 1%, unless you want >= 1%
    if percent > 1:
        color_hex.append(color)
        color_count.append(count)
        color_percent.append(percent)

df['color'] = color_hex
df['count'] = color_count
df['percent'] = color_percent
df['color_name'] = df['color'].map(colors_dict)

df.to_excel(r'C:\Users\User\Desktop\Project\export_dataframe.xlsx',
            index=False, header=True)
print('done')

this is the outcome:

enter image description here

required output

enter image description here



from Increasing the range of colors detected in an image

No comments:

Post a Comment