Created
August 16, 2014 11:54
-
-
Save lubosz/e1b8b2d06b974b7ba196 to your computer and use it in GitHub Desktop.
Simple Mandelbrot Set Visualisation in Python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from numpy import complex, array | |
from PIL import Image | |
import colorsys | |
WIDTH = 1024 | |
def i_to_rgb(i): | |
color = 255 * array(colorsys.hsv_to_rgb(i/255.0, 1.0, 0.5)) | |
return tuple(color.astype(int)) | |
def mandelbrot(x, y): | |
c0 = complex(x, y) | |
c = 0 | |
for i in range(1, 1000): | |
if abs(c) > 2: | |
return i_to_rgb(i) | |
c = c * c + c0 | |
return (0, 0, 0) | |
img = Image.new('RGB', (WIDTH, int(WIDTH/2))) | |
pixels = img.load() | |
for x in range(img.size[0]): | |
print ("%.2f%%" % (x/WIDTH * 100.0)) | |
for y in range(img.size[1]): | |
pixels[x,y] = mandelbrot((x - (0.75 * WIDTH))/(WIDTH/4), | |
(y - (WIDTH/4))/(WIDTH/4)) | |
img.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment