The name of a file in the raster graphics format of your choice. The chosen format must support at least 8 bits per channel and 3 channels.


A file in the same format, with the same dimensions and pixels as the first, but whose pixels are grouped in descending order of the number of times they occur, sorted from left-to-right, top-to-bottom.

  • If certain colours of pixels appear the same number of times, their order is unspecified.
  • You must not overwrite the input file (use a different file name for the output).
  • Any and all third-party image processing libraries are permitted.



Will give output similar to:

sorted pixels from panda image

Especially in the lower parts of the image some variation may occur, due to different tie breaking between colours of equal frequency.

  • 1
    \$\begingroup\$ Can we use other file formats, like PNM, for the output? \$\endgroup\$
    – FUZxxl
    Commented Mar 14, 2015 at 23:19
  • \$\begingroup\$ @EMBLEM I'm not sure what you mean with “forego grayscale.” \$\endgroup\$
    – FUZxxl
    Commented Mar 14, 2015 at 23:25
  • \$\begingroup\$ @MartinBüttner Good point, I've changed it to input and output being the same formats. \$\endgroup\$
    – EMBLEM
    Commented Mar 14, 2015 at 23:26
  • 1
    \$\begingroup\$ @EMBLEM I think he meant “ties” as in “two pixels occur with the same frequency.” Can I assume that their order is unspecified in this case? \$\endgroup\$
    – FUZxxl
    Commented Mar 14, 2015 at 23:30
  • 1
    \$\begingroup\$ To be clear on input/output... can we use a string (file name), a stream of bytes, or a complex File-type object...? That would make a large difference in some languages. \$\endgroup\$
    – Geobits
    Commented Mar 14, 2015 at 23:34

6 Answers 6


Mathematica, 125 123 bytes


This defines an unnamed function which takes the file name in any common image format and writes the result to the a file with the same name but prepended with a. The result looks a bit different from the OP's, since Mathematica's SortBy breaks ties by default sort order, so the bottom bits where loads of ties occur look a bit neater:

Not a panda.

The implementation itself is really straightforward:

  • ImageData to get a grid of colour values.
  • Join to flatten the array.
  • Tally to count the occurrence of each colour.
  • SortBy[...,-Last@#&] to sort by frequencies from highest to lowest.
  • ConstantArray and Join to expand the tallies again.
  • ArrayReshape to recover the original image shape (obtained with Dimensions).
  • Image to convert the data back to an image object.

FYI, 22 bytes are used on file I/O. An equivalent version that takes and returns an image object comes in at 103 bytes:


J, 94 81 bytes

A function taking the name of a PNG file (with no transparency channel) and writing the result in the input filename prepended by "o".

f=.3 :0
(($a)$#/|:\:~(#,{.)/.~,a=.readpng y)writepng'o',y

input output


  • Create a list of every color-number and the number of it's appearances
  • Sort list by number of it's appearances
  • Multiplicate every color-number equal it's appearances
  • Reshape the new list to the shape of the original image
  • \$\begingroup\$ you can save one character if you make the function dyadic and save the sorted PNG into x. \$\endgroup\$
    – FUZxxl
    Commented Mar 15, 2015 at 12:02
  • \$\begingroup\$ @FUZxxl It would actually save 3 chars but I don't think that's in line with the specification. I could also save the image to a 1-letter extensionless file but that's not too elegant. \$\endgroup\$
    – randomra
    Commented Mar 15, 2015 at 13:07
  • \$\begingroup\$ @FUZxxl Well, turned out there was quite some extra chars in the body of the function... \$\endgroup\$
    – randomra
    Commented Mar 17, 2015 at 10:59
  • \$\begingroup\$ I don't speak J, but in the specification there is a string as input. Here the input file name is hardcoded in the program. \$\endgroup\$
    – edc65
    Commented Mar 17, 2015 at 11:01
  • \$\begingroup\$ @edc65 In J if you define a function it's argument is always accessed by the y character. (If you define a function with two arguments those are accessed by x and y and you cannot define functions with more arguments.) \$\endgroup\$
    – randomra
    Commented Mar 17, 2015 at 11:04

Python2/PIL, 244 226 225 223 222 202 186 182 170 159

from PIL.Image import*
s=raw_input();i=open(s);g=list(i.getdata());i.putdata(sum([[c[1]]*-c[0]for c in sorted((-g.count(r),r)for r in set(g))],[]));i.save(2*s)


<p><b>Edit 1:</b> saved 18 bytes with new algorithm.</p>
<p><b>Edit 2:</b> saved 1 byte with remembering that Python 2 has int division without <code>from __future__ import division</code>. Whoops.</p>
<p><b>Edit 3:</b> saved 2 bytes by swapping <code>cmp</code> for <code>key</code>.</p>
<p><b>Edit 4:</b> saved 1 byte by clever unpacking. <code>w=i.size[0]</code> -> <code>w,h=i.size</code></p>
<p><b>Edit 5:</b> moved <code>list()</code> to an earlier position, drastically improving runtime. No length change.</p>
<p><b>Edit 6:</b> saved 20 bytes by remembering that there is also sequence sorting in Python. Probably slower, but also produces images more like the the Mathematica one.</p>
<p><b>Edit 7:</b> saved 16 bytes by changing <code>putpixel</code> to a list and <code>putdata</code>. Under 200!</p>
<p><b>Edit 8:</b> saved 4 bytes by changing <code>range(...)</code> to <code>...*[0]</code> since we don't need the values.</p>
<p><b>Edit 9:</b> saved 12 bytes by converting the <code>for</code> loop to a <code>sum</code>.</p>
<p><b>Edit 10:</b> saved 11 bytes by noticing that the image size doesn't matter anymore (since edit 7). Whoops v2.0.</p>

Shorter version by stokastic, 123

from PIL.Image import*
s=raw_input();i=open(s);d=list(i.getdata());i.putdata(sorted(d,key=lambda D:d.count(D)));i.save(2*s)

Well, let's at least try, even though it's already beaten.

It's extremely slow, Panda processed for several minutes on my laptop.

Saves with a filename with the original filename repeated twice.


  • \$\begingroup\$ you can save 32 bytes by using i=open(raw_input());d=list(i.getdata());i.putdata(sorted(d,key=lambda D:d.count(D)));i.save('o.png'), although it is very very slow for large images (calls list.count on each pixel). \$\endgroup\$
    – stokastic
    Commented Mar 16, 2015 at 17:44
  • \$\begingroup\$ @stokastic Well, I'll add that but credit you for it. I prefer reading the name to a variable (what if the user inputs an image called o.png?) \$\endgroup\$ Commented Mar 17, 2015 at 6:52
  • \$\begingroup\$ fair enough! :) \$\endgroup\$
    – stokastic
    Commented Mar 17, 2015 at 19:17

Python, 1197 bytes

# -*- coding: utf-8 -*-
from __future__ import division
from collections import Counter
import png
from sys import argv,exit
def group(s,n):
    return zip(*[iter(s)]*n)
def flatten(list_of_lists):
    for lst in list_of_lists:
    return result
def group_list(old_list,tuples_per_list):
    for item in old_list:
        if i==tuples_per_list:
    return new_list
if image_data[3]["alpha"]:
elif image_data[3]["greyscale"]:
colours=Counter(colour for row in pixels for colour in group(row,ints_per_colour)).most_common()
pixel_list=flatten([element]*count for element,count in colours)

The png module I used.

  • 2
    \$\begingroup\$ Thank you for providing a reference solution! We usually put these into the challenge post itself so people can find them more easily, but posting them as an answer is okay, too. \$\endgroup\$
    – FUZxxl
    Commented Mar 14, 2015 at 23:20

C# 413

Complete program. Pass the file name on command line, the output is saved in the same format as file "o".

Not using some neat features of linq, like SelectMany and Enumerable.Range, as the program would be cleaner but longer.

using System.Collections.Generic;using System.Linq;using System.Drawing;
class P{
static void Main(string[]a){
var d=new Dictionary<Color,int>();var b=new Bitmap(a[0]);
int n,x,y,w=b.Width,h=b.Height;
for (x=w;x-->0;)for(y=h;y-->0;){var p=b.GetPixel(x,y);d[p]=d.ContainsKey(p)?d[p]+1:1;}
y=h;foreach(var q in d.OrderBy(v=>v.Value)){for(n=q.Value;n-->0;){
if(x<=0){x=w;--y;}b.SetPixel(--x, y, q.Key);}}b.Save("o");

Readable formatting courtesy of VS2010

using System.Collections.Generic;
using System.Linq;
using System.Drawing;

class P
    static void Main(string[] a)
        var d = new Dictionary<Color, int>();
        var b = new Bitmap(a[0]);
        int n,x,y,w = b.Width, h=b.Height;

        for (x = w; x-- > 0;)    
            for (y = h; y-- > 0;)
                var p = b.GetPixel(x, y);
                d[p] = d.ContainsKey(p) ? d[p]+1 : 1;
        y = h;
        foreach (var q in d.OrderBy(v => v.Value))
            for (n = q.Value; n-- > 0; )
                if (x <= 0)
                    x = w;
                 b.SetPixel(--x, y, q.Key);
  • \$\begingroup\$ May be I am wrong but I think you can remove the newlines after { chars \$\endgroup\$
    – sergiol
    Commented Apr 5, 2018 at 20:05
  • \$\begingroup\$ And before the } chars \$\endgroup\$
    – sergiol
    Commented Apr 5, 2018 at 20:06
  • \$\begingroup\$ I quess something like tio.run/##VY/Ba8IwGMX/… would work! \$\endgroup\$
    – sergiol
    Commented Apr 5, 2018 at 20:09

Python 2 : 191 bytes

Here's my attempt. I figured I might be able to save some space by using Counter, but it didn't end up as small as Pietu1998's answer.

from collections import Counter
from PIL import Image
s=reduce(lambda x,y:x+y,map(lambda(a,b):[a]*b,Counter(i.getdata()).most_common()))

Output from Panda

Output from Panda


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.