Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP or Imagemagick : Number of Main Colors From an Image

My Problem My customers are uploading images to put on a t-shirt. I need to know how many main colors are within the design. I have tried PHP scripts and Imagemagick , I can't seem to get the results I am looking for.

This image has 5 main color variations. When I use imagemagick's -unique-colors, I get a huge range of different colors. Is there a line of code or script that I can use to get an outcome of 5.
python logo

Here is the code I am using to try and get unique color count with imagemagick but I get way to many colors.

exec(convert $origimage -unique-colors -scale 1000% $newimage);
like image 374
bbullis Avatar asked Feb 26 '13 10:02

bbullis


1 Answers

Reading this discussion should help you : http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=12818

Basically, you should use imagemagic to generate an histogram of the color used, and sort the result by occurences (or percentage if you prefer) Then you can decide to take top 2 lines and it should give you a not so bad answer.

A more robust algorithm could be to choose a threshold of the percentage values, and keep the colors being above this threshold...

I hope this gives you some clues.

like image 68
Orabîg Avatar answered Oct 25 '22 01:10

Orabîg