Welcome, guest | Sign In | My Account | Store | Cart

Mandelbrot fractal using Python Image Library (PIL).

Python, 26 lines
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26``` ```# Mandelbrot fractal # FB - 201003254 from PIL import Image # drawing area xa = -2.0 xb = 1.0 ya = -1.5 yb = 1.5 maxIt = 255 # max iterations allowed # image size imgx = 512 imgy = 512 image = Image.new("RGB", (imgx, imgy)) for y in range(imgy): zy = y * (yb - ya) / (imgy - 1) + ya for x in range(imgx): zx = x * (xb - xa) / (imgx - 1) + xa z = zx + zy * 1j c = z for i in range(maxIt): if abs(z) > 2.0: break z = z * z + c image.putpixel((x, y), (i % 4 * 64, i % 8 * 32, i % 16 * 16)) image.save("mandel.png", "PNG") ```

FB36 (author) 14 years, 1 month ago

The import statement must be replaced w/ these:

``````from PIL import Image
from PIL import ImageDraw
``````
FB36 (author) 14 years, 1 month ago

Here is a slightly improved version:

``````# Mandelbrot fractal
# FB - 201003151
from PIL import Image
# drawing area (xa < xb and ya < yb)
xa = -2.0
xb = 1.0
ya = -1.5
yb = 1.5
maxIt = 256 # iterations
# image size
imgx = 512
imgy = 512
image = Image.new("RGB", (imgx, imgy))

for y in range(imgy):
cy = y * (yb - ya) / (imgy - 1)  + ya
for x in range(imgx):
cx = x * (xb - xa) / (imgx - 1) + xa
c = complex(cx, cy)
z = 0
for i in range(maxIt):
if abs(z) > 2.0: break
z = z * z + c
r = i % 4 * 64
g = i % 8 * 32
b = i % 16 * 16
image.putpixel((x, y), b * 65536 + g * 256 + r)

image.save("mandel.png", "PNG")
``````
FB36 (author) 14 years, 1 month ago

And here is the Julia fractal version that draws a random Julia set everytime:

``````# Julia fractal
# FB - 201003151
from PIL import Image
import random
# drawing area (xa < xb and ya < yb)
xa = -2.0
xb = 1.0
ya = -1.5
yb = 1.5
maxIt = 256 # iterations
# image size
imgx = 512
imgy = 512
image = Image.new("RGB", (imgx, imgy))
# Julia set to draw
c = complex(random.random() * 2.0 - 1.0, random.random() - 0.5)

for y in range(imgy):
zy = y * (yb - ya) / (imgy - 1)  + ya
for x in range(imgx):
zx = x * (xb - xa) / (imgx - 1) + xa
z = complex(zx, zy)
for i in range(maxIt):
if abs(z) > 2.0: break
z = z * z + c
r = i % 4 * 64
g = i % 8 * 32
b = i % 16 * 16
image.putpixel((x, y), b * 65536 + g * 256 + r)

image.save("julia.png", "PNG")
``````
Andrew Lewis 14 years ago

This is a version of the Mandelbrot fractal code, optimized for speed. This does increase speed at the expense of memory, but the change from range to xrange should soak up some of the slack.

``````# Mandelbrot fractal
# FB - 201003151
# Modified Andrew Lewis 2010/04/06
from PIL import Image
# drawing area (xa < xb and ya < yb)
xa = -2.0
xb = 1.0
ya = -1.5
yb = 1.5
maxIt = 256 # iterations
# image size
imgx = 512
imgy = 512

#create mtx for optimized access
image = Image.new("RGB", (imgx, imgy))

#optimizations
lutx = [j * (xb-xa) / (imgx - 1) + xa for j in xrange(imgx)]

for y in xrange(imgy):
cy = y * (yb - ya) / (imgy - 1)  + ya
for x in xrange(imgx):
c = complex(lutx[x], cy)
z = 0
for i in xrange(maxIt):
if abs(z) > 2.0: break
z = z * z + c
r = i % 4 * 64
g = i % 8 * 32
b = i % 16 * 16
mtx[x, y] =  r,g,b

image.save("mandel.png", "PNG")
``````
 Created by FB36 on Sun, 14 Mar 2010 (MIT)