Detect Main Color of Image
Posted: 2010-08-27T10:12:50-07:00
Hello,
I am writing a script to detect the most predominant color of an image. And it works great,
I'm currently using the convert script in this way:
convert "download/file.php?avatar=1663_1180055717.gif" -colors 16 -geometry x50 histogram:- | identify -format %c - | sort -r > all_colors_for_this_image.txt
Then I do a regular expression to find the first color in the text file all_colors_for_this_image.txt
This method works brilliantly, but what I'm finding is that I get too many colors back. Instead of one specific color, I wish to generalize the color. So that instead of getting 100 blacks (#000000, #000001 etc) the colors are rounded to the closest whatever.
I can probably do this using a mathematical algorithm by rounding down the RGB values, and I'm sure it would be a solution, but not a very good one.
So my question is - is there a way to take only a certain amount of colors, and merge many colors into one?
I tried making the image only 16 colors as you can see, but it didnt work so well I also found that if I made this variable 4 colors then it would return lots of grays.
Thanks!
Andrew
p.s. I've done some research online, and I've looked in the docs, but have not found a solution yet
Maybe I looked in the wrong place
I am writing a script to detect the most predominant color of an image. And it works great,
I'm currently using the convert script in this way:
convert "download/file.php?avatar=1663_1180055717.gif" -colors 16 -geometry x50 histogram:- | identify -format %c - | sort -r > all_colors_for_this_image.txt
Then I do a regular expression to find the first color in the text file all_colors_for_this_image.txt
This method works brilliantly, but what I'm finding is that I get too many colors back. Instead of one specific color, I wish to generalize the color. So that instead of getting 100 blacks (#000000, #000001 etc) the colors are rounded to the closest whatever.
I can probably do this using a mathematical algorithm by rounding down the RGB values, and I'm sure it would be a solution, but not a very good one.
So my question is - is there a way to take only a certain amount of colors, and merge many colors into one?
I tried making the image only 16 colors as you can see, but it didnt work so well I also found that if I made this variable 4 colors then it would return lots of grays.
Thanks!
Andrew
p.s. I've done some research online, and I've looked in the docs, but have not found a solution yet
