Creating Julia sets using Python

473 Views Asked by At

I want to create my own images of the Julia set of the complex function $e^z-2$, similar to the one below:

enter image description here

The simple (Python) program:

import cmath
from PIL import Image 

if __name__ == "__main__": 

w, h, zoom = 1600,1000,1

bitmap = Image.new("RGB", (w, h), "white") 

pix = bitmap.load() 

maxIter = 6

for x in range(w): 
    for y in range(h): 
        zx = 1.0*(x - w/2)/200 + 3 
        zy = 1.0*(y - h/2)/200 + 0
        z=complex(zx,zy)
        i = maxIter 
        while  z.real< 50 and i > 1:
            z=cmath.exp(z)-complex(2,0)
            i -= 1
        if i==1:
            pix[x,y] = 255,255,255
        if i>1:
            pix[x,y] = 0,0,0

bitmap.show()

produces this image:

enter image description here

Essentially, the Julia set of this function is just the set of escaping points. So the idea for my program was to test the first few iterates of the function on each point $z$, and see if any of those iterates is large (in modulus or just the real part). But clearly it's not good enough. How can I improve it to get the top image? Or, can you give a new Python code to produce the top image?

2

There are 2 best solutions below

1
On

It looks like your black pixels represent points which do not escape, where "escape" is characterized in your program by having a real part exceeding 50 by the 6th iteration.

If you want more points to qualify as escapees, you can either (1) increase the number (now 6) of iterations, or (2) decrease the minimum real part (now 50) required to decide an iterate has escaped.

It may possibly help efficiency to combine two iterations in a single computation (but I'm not sure if that's true).

I hope this helps.

3
On

On first look, your code looks ok. I've experimented with Cantor Bouquets alot, (that's what these Julias ar called) and found that the bailout value needs to be really high to clear the internal regions and push the solid boundary back. Think in the range of $10^{50}$ to $10^{80}$ for some really solid resolution. I don't know if Python can handle such large numbers. I used Maple and the results are satisfying. Note that if you use too high a number of iterations, you'll lose threads as well. The balance seems to be around 100-200 iterations with a bailout of round the range I gave above. Good luck.