Culme
Culme

Reputation: 1095

Counting and identifying colours in a vector image using ImageMagick

Customers upload image files, typically logos, to a web site, and I would like to be able to identify which colours the images contain. I have kindof given up on bitmap images, since the anti aliasing introduces so many variations of each colour, but for vector images (eps, svg, ai to mention a few that could occur) I want to believe it should be doable.

The ideal solution would enable me to produce a list of colours which the user can verify; "Your image contains 3 colours: 111c, 222c and 333c, are these the colours you would like to use for printing?"

I am using Magick.net and C#. I am able to read the files into "MagickImage" instances, but I am lost on how to proceed in identifying the colours.

Upvotes: 0

Views: 495

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207728

Let's say you start with this image, which is made from random colours then reduced down to just 18 colours:

convert -size 256x256 xc:black +noise random -colors 18 image.png

enter image description here

Now, you can get a list of the colours like this:

convert image.png -unique-colors -depth 8 txt:

Sample Output

# ImageMagick pixel enumeration: 18,1,65535,srgb
0,0: (7967,24415,7967)  #1F5F1F  srgb(31,95,31)
1,0: (24415,24672,8481)  #5F6021  srgb(95,96,33)
2,0: (16191,12336,16448)  #3F3040  srgb(63,48,64)
3,0: (8224,8224,24158)  #20205E  srgb(32,32,94)
4,0: (24672,24415,24415)  #605F5F  srgb(96,95,95)
5,0: (49344,16191,16448)  #C03F40  srgb(192,63,64)
6,0: (16448,46260,13878)  #40B436  srgb(64,180,54)
7,0: (8224,57311,24415)  #20DF5F  srgb(32,223,95)
8,0: (24415,57568,24415)  #5FE05F  srgb(95,224,95)
9,0: (49087,49344,16448)  #BFC040  srgb(191,192,64)
10,0: (13364,16448,46517)  #3440B5  srgb(52,64,181)
11,0: (24415,8224,57311)  #5F20DF  srgb(95,32,223)
12,0: (24415,24415,57054)  #5F5FDE  srgb(95,95,222)
13,0: (40863,24672,40863)  #9F609F  srgb(159,96,159)
14,0: (50372,15163,50115)  #C43BC3  srgb(196,59,195)
15,0: (16448,49087,49344)  #40BFC0  srgb(64,191,192)
16,0: (41120,40863,41120)  #A09FA0  srgb(160,159,160)
17,0: (50372,50372,50372)  #C4C4C4  grey77

And maybe you would like a swatch too:

convert image.png -unique-colors -scale 400x40 swatch.png

enter image description here

Upvotes: 4

Related Questions