Skip to content

Instantly share code, notes, and snippets.

@aciceri
Last active August 29, 2015 14:20
Show Gist options
  • Save aciceri/2bdd81ae1e96612424c1 to your computer and use it in GitHub Desktop.
Save aciceri/2bdd81ae1e96612424c1 to your computer and use it in GitHub Desktop.
from PIL import Image
from colorsys import hsv_to_rgb
hue2rgb = lambda hue: tuple([int(color * 255) for color in hsv_to_rgb(hue, 1, 1)])
width, height = 500, 500
image = Image.new(mode="RGB", size=(width, height), color=(0, 0, 0))
xa, xb = -2.0, 1.0
ya, yb = -1.5, 1.5
maxstep = 100
for y in range(height):
for x in range(width):
c = complex(xa + (xb - xa) * x / width, ya + (yb - ya) * y / height)
z = complex(0, 0)
for i in range(maxstep):
z = z ** 2 + c
if abs(z) >= 2:
image.putpixel((x, y), hue2rgb(i / float(maxstep)))
break
print("Riga n. %d di %d completata" % (y+1, height))
image.save("mandelbrot.png")
image.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment