6

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);
Community
  • 1
  • 1
bbullis
  • 532
  • 7
  • 20
  • But when printing a shirt, I would just be printing just 1 blue and 1 yellow. To the human eye that iamge looks like 2 colors but a script will pick up more than two colors, I want the human eye version. Script to be smart enough to that says, there is 1 blue variation and 1 yellow variation. Did you read my question? – bbullis Feb 26 '13 at 10:29
  • I think, what you are looking for is a way to determinate the "main colors" no matter how many true colors are in the picture. I would recommend to use a software like Paintshop or pixlr.com and then simply save the image as GIF with an reduced palette of 3 colors (white/blue/yellow). – powtac Feb 26 '13 at 10:34
  • haha are you kidding me? Did you read my question or are you just writing comments to boost your reputation? That is what I said I need the main colors right in my question and the title of my question. I need this all done through a script, not photoshop. – bbullis Feb 26 '13 at 10:37
  • there are many duplicates here..on is [http://stackoverflow.com/questions/10290259/detect-main-colors-in-an-image-with-php](http://stackoverflow.com/questions/10290259/detect-main-colors-in-an-image-with-php) – bitWorking Feb 26 '13 at 10:44
  • I have seen this question before and have tried that script, but I have not gotten results I am looking for. That script doesn't successful work on all of the images I have tried. – bbullis Feb 26 '13 at 10:45

2 Answers2

2

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.

Orabîg
  • 11,718
  • 6
  • 38
  • 58
  • Thanks this info. I ended up rigging a solution based on some of the info from that forum. I am basically getting every pixel color with the histogram, parsing the histogram output and only counting the colors that show up in more than 5% of the photo. For my purposes, it works! Screen printing for t-shirts don't allow gradients so I am set and ready to roll! Thanks. – bbullis Feb 26 '13 at 13:08
  • Happy to see it's working. In your case 5% is a good threshold, but keep in mind that you can make it vary depending on the input image. Anyway, don't forget to validate the answer if it's good for you. – Orabîg Feb 26 '13 at 13:37
  • This isn't a good enough solution, I have failed with a couple other images that I tried – bbullis Feb 27 '13 at 22:36
0

You can not determinate the numbers of colors (3 in this example) in before unless you you are aware of the subjective complexity of the image.

There are three settings which render the image with a given number of colors.

convert python-logo.png +dither   -colors 3                   python-logo-reduced-colors.gif
convert python-logo.png +dither   -posterize 3                python-logo-reduced-colors.gif
convert python-logo.png -separate -threshold 50% -combine     python-logo-reduced-colors.gif
powtac
  • 40,542
  • 28
  • 115
  • 170
  • This doesn't help me retrieve the number of colors! I don't know what my customer is going to be uploading, so I can't use -colors 3. Powtac, don't reply or comment unless you understand my question, third time you have posted without reading my question, give it up. – bbullis Feb 26 '13 at 10:58