There are levels of tidiness.
Totally Deranged Tidy.
After sharing these awesome pictures by the Swiss “artist” Ursus Wehrli with the world, Martin N. pointed out that there is still a severe issue with the Badewiese: People are not even sorted by the colors of their swimsuits! Adding to that, trees are placed in total chaos, not to speak of their leaves; clearly, there are tiny waves on the water; and one can almost see blades of grass not being sorted by size. Obviously, this picture needed further tidying up.
Python to the rescue! 5 minutes, 10 lines, and half a beer later I had a program that would load the image, re-arrange its pixels, and save it again:
from PIL import Image old = Image.open("tidy.jpg") colors = old.getcolors(old.size * old.size) data =  for count, color in colors: data.extend(count * [color]) data.sort() new = Image.new('RGB', old.size) new.putdata(data) new.save("naive_tidy.png")
So how was this “re-arrangement” performed? In a naïve approach, I just sorted the RGB values of the pixels, which is why there are many consecutive lines (of equal reds) with abrupt changes. Ordered, but far from being tidy.
Let’s try another color space and sort by HSV values—a smooth hue should look better, after all. So by adding
and changing one line to
data.sort(key=lambda rgb: colorsys.rgb_to_hsv(*rgb))
More beautiful, but the red and white noise in the end doesn’t look tidy at all. We want a smooth transition through all given colors. Hilbert curves to the rescue! Found a ready-to-be-used Hilbert walk implementation, added
and changed the sorting once again to
Mathematical computer science just made the world a little tidier.