Obfuscated mandlebrot function - can anyone deobfus it?

I am new to python and I became very interested in its ability to create fractal images. I wrote some simple ones, but I just discovered a script for Mandelbrot fractals ... it creates beautiful full color images at the desired resolution ... but the code is convoluted to look like ASCII art by Mandelbrot ... really cool but stupid if you want to read it easily. It contains features that I have not yet learned in python, so if anyone can step back from the script to look like a normal python script, that would be great. script:

_                                      =   (
                                        255,
                                      lambda
                               V       ,B,c
                             :c   and Y(V*V+B,B,  c
                               -1)if(abs(V)<6)else
               (              2+c-4*abs(V)**-0.4)/i
                 )  ;v,      x=7200,4800;C=range(v*x
                  );import  struct;P=struct.pack;M,\
            j  ='<QIIHHHH',open('M.bmp','wb').write
for X in j('BM'+P(M,v*x*3+26,26,12,v,x,1,24))or C:
            i  ,Y=_;j(P('BBB',*(lambda T:(T*80+T**9
                  *i-950*T  **99,T*70-880*T**18+701*
                 T  **9     ,T*i**(1-T**45*2)))(sum(
               [              Y(0,(A%3/3.+X%v+(X/v+
                               A/3/3.-x/2)/1j)*2.5
                             /x   -2.7,i)**2 for  \
                               A       in C
                                      [:9]])
                                        /9)
                                       )   )

      

As I said, art is cool, but too hard to read! If someone can do this, I would be very grateful.

+3


source to share


1 answer


Note. This is not meant to be a definitive answer, but as an attempt to phase out obfuscation. If you can provide an extra step to make things clearer, it would be great if you could add it to this answer.

Ok, let's start by typing in the appropriate lines and indentation of the code:

_ = (255,
    lambda V, B, c: c and Y(V*V + B, B, c-1) if(abs(V) < 6) else (2 + c - 4 * abs(V)**-0.4)/i)
v, x = 7200, 4800
C = range(v*x)
import struct
P=struct.pack
M, j = '<QIIHHHH', open('M.bmp','wb').write
for X in j('BM' + P(M, v*x*3+26, 26, 12, v, x, 1, 24)) or C:
    i, Y = _
    j(
        P('BBB',
                *(lambda T: (T * 80 + T**9 * i - 950 * T**99, T*70 - 880* T**18 + 701* T**9, T* i**(1-T**45*2)))(
                    sum([Y(0, (A%3/3. + X%v + (X/v + A/3/3. - x/2)/1j) * 2.5/x - 2.7, i)**2 for A in C[:9]]) / 9
                )
        )
    )

      

Slightly better, but still confusing. For example, defining P

as an alias for struct.pack

and all of these two-declarations in one. If we get rid of them and move the lambda definition outside the loop, we get the following:

import struct
i = 255
Y = lambda V, B, c: c and Y(V*V + B, B, c-1) if(abs(V) < 6) else (2 + c - 4 * abs(V)**-0.4)/i
v = 7200
x = 4800
C = range(v*x)
M = '<QIIHHHH'
color = lambda T: (T * 80 + T**9 * i - 950 * T**99, T*70 - 880* T**18 + 701 * T**9, T * i**(1-T**45*2))

f = open('M.bmp','wb')
for X in f.write('BM' + struct.pack(M, v*x*3+26, 26, 12, v, x, 1, 24)) or C:
    f.write(
        struct.pack('BBB',
            *color(sum([Y(0, (A%3/3. + X%v + (X/v + A/3/3. - x/2)/1j) * 2.5/x - 2.7, i)**2 for A in C[:9]]) / 9))
    )

      



Now everything becomes a little clearer. The loop records the color values ​​for each pixel, and the lambda returns a 3-tuple representing the blue, green, and red values ​​of each pixel.

import struct
i = 255
Y = lambda V, B, c: c and Y(V*V + B, B, c-1) if abs(V) < 6 else (
                    (2 + c - 4 * abs(V)**-0.4)/i)
v = 7200
x = 4800
C = range(v*x)
M = '<QIIHHHH'
color = lambda T: (T * 80 + T**9 * i - 950 * T**99,
                   T*70 - 880* T**18 + 701 * T**9,
                   T * i**(1-T**45*2))
f = open('M.bmp', 'wb')
for X in f.write('BM' + struct.pack(M, v*x*3+26, 26, 12, v, x, 1, 24)) or C:
    f.write(struct.pack('BBB',
        *color(sum(
            [Y(0, (A%3/3. + X%v + (X/v + A/3/3. - x/2)/1j) * 2.5/x - 2.7, i)**2
                for A in C[:9]]) / 9))
    )

      

As per your comment, the asterisk (star, *) unpacks the list into an argument list .

Many hours later, here's a roughly 100MB image taken from the Mandelbrot set :

Mandelbrot set

+3


source







All Articles