Showing posts with label fractal. Show all posts
Showing posts with label fractal. Show all posts

Friday, February 15, 2008

[Python]Draw a fractal pattern(Julia set)

I wrote a program to draw a fractal pattern.
Julia set - Wikipedia, the free encyclopedia.
I found it is very easy to implement; it took much time to check the mathematical definition of Julia set than to implement. If you already know Python and don't know Python Imaging Library (PIL) yet, I strongly recommend to learn it!



Using PIL you can generate images like above from very simple code:


import Image
import ImageDraw

SIZE = 128
image = Image.new("L", (SIZE, SIZE))
d = ImageDraw.Draw(image)

c = 0.5 + 0.2j
for x in range(SIZE):
for y in range(SIZE):
re = (x * 2.0 / SIZE) - 1.0
im = (y * 2.0 / SIZE) - 1.0

z=re+im*1j
for i in range(128):
if abs(z) > 2.0: break
z = z * z + c
d.point((x, y), i * 2)

image.save(r"c:\julia.png", "PNG")


Here is a movie(2MB, c = -0.78 + i * 0.002 + 0.23j (i < 40), center = -0.5 + 0.2j, scale = 1.0). I think this type of movie isn't suitable for MPEG compression, it is raw avi.