# How to count colors in an image or series of images?

Perhaps this question is too technical for this subsite, if so, please relocate it to a better spot.

I'm doing some research on colors where I need to figure out how common colors are. The end result will be a bunch of charts, somewhat similar to this flag analysis.

I will have 50+ sets of 100 or so images and I want to calculate the color distribution in each set, and then compare the averages of each set. Which is the most common color in all sets, are there changes/trends between sets, and so on. With thousands of images obviously it's not feasible to count all pixels myself and decide what colors are most prominent. So I'd like to automate.

Ideally, I'd have a tool that looks at each set and quantizes colors based on a palette I create myself. I'd also love it if the tool would accept a series of different-sized images, but I'm willing to rescale and combine each series in to a single collage.

Note: I'm looking for something that lists PIXEL colors, not IMAGE AVERAGE colors. Since I will have images that have multiple colors, looking at the average or most prominent color gives misleading results. For example, in the following image there are several images that use a rainbow of colors, but do not get placed near eachother.

And this image is predominantly blue, but should still count for about 1/3rd as green pixels:

Or even worse, the following image uses bright green and red, but would either be categorized as yellow (average hue) or grayish (average of RGB values):

To recap: I'm looking for a tool that can reduce an image to a small (specified) color palette, and then count how many pixels of each of those colors there are.

• The tool you use will introduce some bias into the results depending on the image you scan and the means by which you use to down or up sample it to the desired size for pixel counting. Be also aware that counting pixels can be misleading based on pixel size and white-point of the image and display, among other factors.
– Stan
Jun 19, 2016 at 1:15

Interesting question.

On Photoshop:

1) Multiply your image dimensions. For example 640x480= 307,200 Save this number (total value), probably on an excell table.

2) Convert your image to 8 bit

• Mode > 8 bit

• Choose an amount of colors. Lets say the double of the palete you can refine the palete later. Keep it in manageable values. for example 16-24 colors to refine it later to 8.

• Palete Local, dither none.

3) Adjust the palete as needed

• Image > Mode > Color Table

4) Select Color range and choose one or several similar colors using the tolerance slider.

5) Open the histogram and turn on the extended view. Window > Histogram.

• This is now counting the pixels inside the selection.

6) Divide the total value / selected value and you now have the percentage of thoose colors.

7) Repeat as necesary depending on the palete you choosed.

Download IrfanView, a completely free and neat Windows Application. Open your image with it and go to Image -> Information -> 'Number of Unique Colors' field (make sure 'Auto Count' is checked).

This will count the number of colors in your raster image.

If you want to analyze a set of images, you can use ImageMagick function convert, which is able to count the unique pixel colors of the image.

The command is very simple:

convert [your file] -define histogram:unique-colors=true -format %c histogram:info:-

At the moment I'm on a Windows machine, and for your first example I should use:

convert.exe K7Yo6.png -define histogram:unique-colors=true -format %c histogram:info:-

Which outputs:

2237: (  0,  0,  0,255) #000000 black
8: (  0,  0,  2,255) #000002 srgba(0,0,2,1)
2: (  0,  0,  4,255) #000004 srgba(0,0,4,1)
3: (  0,  0,  5,255) #000005 srgba(0,0,5,1)
4: (  0,  0,  7,255) #000007 srgba(0,0,7,1)
[...]

If you want to sort in reverse order the output (to see the most frequent color), you can redirect it on sort command:

convert.exe K7Yo6.png -define histogram:unique-colors=true -format %c histogram:info:- | sort /R

Which outputs:

118590: ( 64, 64, 64,255) #404040 grey25
2237: (  0,  0,  0,255) #000000 black
1963: (  3,  3,  3,255) #030303 grey1
1687: ( 33, 33, 33,255) #212121 grey13
1495: (  8,  8,  8,255) #080808 grey3
1232: (  3,  1, 12,255) #03010C srgba(3,1,12,1)
628: (  3,  0, 22,255) #030016 srgba(3,0,22,1)
[...]

The same for the other example. The command:

convert.exe aEkrE.jpg -define histogram:unique-colors=true -format %c histogram:info:- | sort /R

Outputs:

60155: ( 30, 96,182) #1E60B6 srgb(30,96,182)
43676: ( 34,101,182) #2265B6 srgb(34,101,182)
42031: ( 36,103,184) #2467B8 srgb(36,103,184)
38782: ( 44,111,192) #2C6FC0 srgb(44,111,192)
38469: ( 14, 80,167) #0E50A7 srgb(14,80,167)
36962: ( 41,108,189) #296CBD srgb(41,108,189)
35717: ( 20, 86,172) #1456AC srgb(20,86,172)
35554: ( 40,107,188) #286BBC srgb(40,107,188)
[...]

If you need to process a lot of images, you can redirect each output on a file, e.g.:

convert.exe K7Yo6.png -define histogram:unique-colors=true -format %c histogram:info:- | sort /R > K7Yo6.txt

In Linux and macOS you may use sort -r -k 1 to sort the output.

convert K7Yo6.png -define histogram:unique-colors=true -format %c histogram:info:- | sort -r -k 1 > K7Yo6.txt

• Best answer here. Far easier IMO than mathing off of a histogram. To make the most out of it, you'll need to reduce the colors in your image. To do this in Photoshop: Image > Mode > Indexed Color, and toy with Forced to get Colors to the fewest possible (Primaries may work well to get it down to 8) May 12, 2020 at 18:17

The best statistical tool for this kind of pixel analysis is a "histogram." Typical ones divide an image vertically by percentage and 256 columns wide for each of different luminances from black to white. You can capture a histogram for each of the image colour channels you wish to compare. They can be viewed separately by channels such as red, green, blue, and combinations of those. Hue, saturation, and value are interrelated components of a colour.

Photographers have a histogram to assess the image displayed on the camera screen after an image is captured. Some software displays histograms also. The shapes will be different for each of the image samples you show. Your link shows a project with a limited palette of a few basic flat colours. Your gamut is much larger and more nuanced.

Histograms will not be so distinctive as the simple flat colours used on flag designs in your link; but, differences will be more evident as they are overlaid upon one another to accentuate variations.

Make a histogram for comparison here

There's a more detailed description of the process and some variations.