Skip to content

Instantly share code, notes, and snippets.

@WhiteHotLoveTiger
Created November 17, 2014 20:24
Show Gist options
  • Save WhiteHotLoveTiger/3d21a24e055c5fe5d2c9 to your computer and use it in GitHub Desktop.
Save WhiteHotLoveTiger/3d21a24e055c5fe5d2c9 to your computer and use it in GitHub Desktop.
Creates a greyscale image of the Mandelbrot set.
# Modified from http://forthescience.org/blog/2010/07/12/the-mandelbrot-set-in-python/
from PIL import Image
max_iteration = 100
x_center = -1.0
y_center = 0.0
size = 3000
im = Image.new("RGB", (size,size))
for i in xrange(size):
for j in xrange(size):
x,y = ( x_center + 4.0*float(i-size/2)/size,
y_center + 4.0*float(j-size/2)/size
)
a,b = (0.0, 0.0)
iteration = 0
while (a**2 + b**2 <= 4.0 and iteration < max_iteration):
a,b = a**2 - b**2 + x, 2*a*b + y
iteration += 1
if iteration == max_iteration:
color_value = 0
else:
color_value = 255 - (iteration*10 % 255)
im.putpixel( (i,j), (color_value, color_value, color_value))
im.save("mandelbrot.png", "PNG")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment