Average Colours of Image with Multiple Colours

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Posts: 21
Joined: 2018-10-30T09:49:10-07:00
Authentication code: 1152

Average Colours of Image with Multiple Colours

Post by FUrn » 2019-11-08T03:08:22-07:00

I have a number of image swatches that I would like to auto-detect RGB values and output those to a CSV file - I currently use the following code to do this:

Code: Select all

magick *.jpg -format "%f,%[fx:int(mean.r*255)],%[fx:int(mean.g*255)],%[fx:int(mean.b*255)]\n" info: > swatchesRGB.csv
However whilst this works well for single-colour swatches, it doesn't suit my needs for swatches with more than one distinct colour. For example it works fine for these swatches:

Image and Image and Image

But not for these:

Image and Image

How can I add a check for number of (distinct) colours, and then output the RGB values for each of those colours? So for the first three examples it would recognise that, despite lighting and texture, the swatches are pretty much one colour, whilst for the latter two swatches there are two distinct colours?

I am using using ImageMagick 7.0.8-14 on a Windows PC, with BASH syntax.

Posts: 12301
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Average Colours of Image with Multiple Colours

Post by snibgo » 2019-11-08T05:02:45-07:00

You can count the number of unique colours, and list them in "txt:" format, like this:

Code: Select all

f:\web\im>%IMG7%magick toes.png -unique-colors -format "NumCols=%w\n" +write info: txt:
# ImageMagick pixel enumeration: 62206,1,65535,srgb
0,0: (8200,6014,7933)  #2008177E1EFD  srgb(12.5124%,9.17678%,12.105%)
1,0: (11597,5614,7694)  #2D4D15EE1E0E  srgb(17.6959%,8.56641%,11.7403%)
2,0: (9697,7933,6305)  #25E11EFD18A1  srgb(14.7967%,12.105%,9.62081%)
3,0: (10047,6854,8045)  #273F1AC61F6D  srgb(15.3307%,10.4585%,12.2759%)
4,0: (11489,7029,8085)  #2CE11B751F95  srgb(17.5311%,10.7256%,12.3369%)
(I show just the first few lines of output.)

In a script, you could check the number of colours, and do whatever processing you want.
snibgo's IM pages: im.snibgo.com

User avatar
Posts: 25664
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Average Colours of Image with Multiple Colours

Post by fmw42 » 2019-11-08T10:48:33-07:00

I would suggest you reduce the number of colors (+dither -colors 8, for example). Then get the histogram. Sort by count. Take the top N colors.

Code: Select all

magick logo: +dither -colors 8 -format "%c" histogram:info: | sort -k 1,1 -n -r | head -n 4
    257422: (255,255,255) #FFFFFF srgb(255,255,255)
     16375: ( 34, 61,145) #223D91 srgb(34,61,145)
     14329: ( 29, 30, 51) #1D1E33 srgb(29,30,51)
      6000: (233,187, 75) #E9BB4B srgb(233,187,75)
or if you just want the colors

Code: Select all

magick logo: +dither -colors 8 -format "%c" histogram:info: | sort -k 1,1 -n -r | head -n 4 | sed -n 's/.*\(srgb.*\)/\1/p'


Post Reply