August 22, 2011
8

Penrose Tiling in Obfuscated Python

Who says you can’t write obfuscated Python?

Here’s a Python script which renders some Penrose tiling. Yes, this is valid Python code:

_                                 =\
                                """if!
                              1:"e,V=100
                            0,(0j-1)**-.2;
                           v,S=.5/  V.real,
                         [(0,0,4      *e,4*e*
                       V)];w=1          -v"def!
                      E(T,A,              B,C):P
                  ,Q,R=B*w+                A*v,B*w+C
            *v,A*w+B*v;retur              n[(1,Q,C,A),(1,P
     ,Q,B),(0,Q,P,A)]*T+[(0,C            ,R,B),(1,R,C,A)]*(1-T)"f
or!i!in!_[:11]:S       =sum([E          (*x)for       !x!in!S],[])"imp
  ort!cair               o!as!O;      s=O.Ima               geSurfac
   e(1,e,e)               ;c=O.Con  text(s);               M,L,G=c.
     move_to                ,c.line_to,c.s                et_sour
       ce_rgb                a"def!z(f,a)                :f(-a.
        imag,a.       real-e-e)"for!T,A,B,C!in[i       !for!i!
          in!S!if!i[""";exec(reduce(lambda x,i:x.replace(chr
           (i),"\n "[34-i:]),   range(   35),_+"""0]]:z(M,A
             );z(L,B);z         (L,C);         c.close_pa
             th()"G             (.4,.3             ,1);c.
             paint(             );G(.7             ,.7,1)
             ;c.fil             l()"fo             r!i!in
             !range             (9):"!             g=1-i/
             8;d=i/          4*g;G(d,d,d,          1-g*.8
             )"!def     !y(f,a):z(f,a+(1+2j)*(     1j**(i
             /2.))*g)"!for!T,A,B,C!in!S:y(M,C);y(L,A);y(M
             ,A);y(L,B)"!c.st            roke()"s.write_t
             o_png('pen                        rose.png')
             """                                       ))

When this program runs, it outputs a 1000×1000 image file to penrose.png, consisting of about 2212 Penrose tiles rendered with a 3D relief effect. Here’s a slice of the image (click to enlarge):

The script requires Pycairo. It only runs on Python <= 2.7; Python 3 is not supported. It started life as a regular Python script, but in my effort to make the code more compact, I got a bit carried away.

Penrose tilings are cool because they cover the entire plane in an aperiodic way — a shifted copy of the image never matches the original. They were invented by Sir Roger Penrose after a series of attempts to tile the plane with pentagonal shapes. For an explanation of the algorithm behind this script, see my next post, Penrose Tiling Explained.

Python never got much credit as an obfuscated programming language, compared to C or Perl. It seems a contest never took place, and there aren’t too many examples of obfuscated Python on the web: You’ll find a few examples in the official Python FAQ and on various pages such as here and here. There was also a talk at PyCon 2011.

I believe this is the first example of obfuscated Python which outputs a high-resolution image. You’ll find another example in my followup post, High-Resolution Mandelbrot in Obfuscated Python. If you know of any others, let me know in the comments!

8 Comments

  • Michael Harwood August 22, 2011 @ 8:34 am

    This is beautiful and amazing!

    I would like a program that allows me to play around with Penrose Tiling. I want to select which piece goes where and to toggle the colour. I can’t find anything that will do this and Penrose tilings are too hard for me to program on my own. I would like a program that lets you drag and snap a piece onto an existing location (that also forbids overlaps…). The only programs that I have found do a whole Penrose tiling surface at once. There is no user interaction.

    Could you do this?

    Reply

  • joe-random August 22, 2011 @ 8:55 am

    Got a bit carried away, ha?

    Quite cool, but don’t expect it to be appreciated by religious pythonistas.

    You can write obfuscated code in any language, some just actively try to make it harder.

    Reply

  • Simon Chester August 22, 2011 @ 10:09 am
  • RichTea August 22, 2011 @ 10:57 am

    Fantastic! now can you do the output as an SVG ;)

    Reply

  • Huevoos August 22, 2011 @ 12:20 pm

    That’s great!
    Would you mind posting the unobfuscated code?
    I’d love to take a look at your algorithm and, if it’s ok with you, play with it on another language :D

    Reply

  • joey August 22, 2011 @ 1:46 pm

    but most of it’s just a string.. so it’s easy to do anything with it :/

    Reply

  • jim bob August 22, 2011 @ 1:50 pm

    Sorry my last reply lost the formatting.

    I explained how to unobsfucate the code on reddit:

    http://www.reddit.com/r/programming/comments/jqi9d/who_says_you_cant_write_obfuscated_python/c2ecprp

    and wrote code to save to a SVG here:

    http://www.reddit.com/r/programming/comments/jqi9d/who_says_you_cant_write_obfuscated_python/c2ed2hb

    basically change ImageSurface(1, e, e) to SVGSurface(“p.svg”, e, e) and change s.write_to_png to s.finish() at the end.

    Reply

Leave a Reply

*