Color count, sorted by occurences

Bash + coreutils, 54

  • 7 bytes saved thanks to @manatwork
grep -oE '#\w+'|sort|uniq -c|sort -nr|awk '$0=$2" "$1'

This assumes STDIN input of Imagemagick's .txt format.

Ideone.


If the above input format is too much of a stretch, then we can add Imagemagick conversion from any format:

Bash + coreutils + Imagemagick, 71

convert $1 txt:-|grep -oE '#\w+'|sort|uniq -c|sort -nr|awk '$0=$2" "$1'

Here, the input image filename is passed in as a command-line parameter.


Mathematica, 91 bytes

StringRiffle@SortBy[Tally["#"<>IntegerString[#,16,2]&/@Join@@#~ImageData~"Byte"],-Last@#&]&

Uses a completely different method from @DavidC's answer. Unlike that one, this can support images with any size and color count. Explanation:

#~ImageData~"Byte"                   Take the RGB values of the image...
Join@@(...)                          Flatten to a list of colors...
"#"<>IntegerString[#,16,2]&/@(...)   Convert each color to #012def format...
Tally[(...)]                         Convert to a list of {color, multiplicity}
                                      pairs (this does most of the work)...
SortBy[(...),-Last@#&]               Order by the multiplicity descending...
StringRiffle@(...)                   And create a string with the desired format.

JavaScript (ES6), 359 355 bytes

Saved 4 bytes thanks to @Neil

u=>{i=new Image;i.src=u;e=document.createElement`canvas`;c=e.getContext`2d`;i.onload=_=>{w=e.width=i.width;h=e.height=i.height;c.drawImage(i,0,0);d=c.getImageData(0,0,w,h).data;for(o={},i=0;i<d.length;i+=4)++o[s='#'+`00000${(d[i]<<16|d[i+1]<<8|d[i+2]).toString(16)} `.slice(-7)]?0:o[s]=1;Object.keys(o).sort((a,b)=>o[b]-o[a]).map(c=>console.log(c+o[c]))}}

Demo

f=u=>{i=new Image;i.crossOrigin='';i.src=u;e=document.createElement`canvas`;c=e.getContext`2d`;i.onload=_=>{w=e.width=i.width;h=e.height=i.height;c.drawImage(i,0,0);d=c.getImageData(0,0,w,h).data;for(o={},i=0;i<d.length;i+=4)++o[s='#'+`00000${(d[i]<<16|d[i+1]<<8|d[i+2]).toString(16)} `.slice(-7)]?0:o[s]=1;Object.keys(o).sort((a,b)=>o[b]-o[a]).map(c=>console.log(c+o[c]))}}
f('http://i.imgur.com/acPudA9.gif')
<input value="https://i.imgur.com/acPudA9.gif" onchange="console.log('-------');f(this.value)">

Other test cases I uploaded to imgur to support CORS:

  • https://i.imgur.com/OgV45Vc.png
  • https://i.imgur.com/n3BhpJi.png

The specific color data on these two seems to have altered slightly when uploading for some reason but it still prints the same amount of occurrences for those two test cases.