If you have the command-line tool imagemagick installed, you can use the following command in *nix or OSX terminal:
convert <filename> txt:- | cut -d " " -f 4 | uniq -c
replacing <filename>
with the name of your file. This will print a list of counts and hex colors, like:
10 #4A413F
14 #483E3C
This works by using the convert
tool to convert the file to "txt" format, which is made up of lines like:
9,0: (75,74,76) #4B4A4C srgb(75,74,76)
Which is the x, y coords, then the decimal RGB values, the hex value, and then the srgb value of the pixel. Obviously, this format is not very efficient :). We write this to standard out (txt:-
means "write txt format to stdout").
Then, we use the built-in cut
command to grab the fourth (-f 4
) column, delimited on spaces (-d " "
), which here is the hex value (interestingly, there are two spaces between the decimal RGB and the hex value, so if you want the decimal RGB, use -f 2
).
Finally, we use the built-in uniq
command to count -c
the unique lines, and print it to standard out (the terminal).