Advertisement
Gutawer

mandel.py

Jan 24th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. import cmath
  2. import math
  3. import time
  4. import numpy as np
  5. from PIL import Image
  6. import sys
  7.  
  8.  
  9. def frange(start, end=None, inc=None):
  10.     # A range function, that does accept float increments...
  11.  
  12.     if end is None:
  13.         end = start + 0.0
  14.         start = 0.0
  15.  
  16.     if inc is None:
  17.         inc = 1.0
  18.  
  19.     L = []
  20.     while 1:
  21.         next = start + len(L) * inc
  22.         if inc > 0 and next >= end:
  23.             break
  24.         elif inc < 0 and next <= end:
  25.             break
  26.         L.append(next)
  27.  
  28.     return L
  29.  
  30.  
  31. def main():
  32.     c = complex(0, 0j)
  33.     z = complex(0, 0j)
  34.  
  35.     iterations = 0
  36.     iterations_max = 1000
  37.  
  38.     image_res = [int(input("Horz. Res: ")), int(input("Vert. Res: "))]
  39.     ranges = [0, 0]
  40.  
  41.     # image is long
  42.     if (image_res[0] > image_res[1]):
  43.         ranges = [2*(image_res[0]/image_res[1]), 2]
  44.     # image is tall
  45.     elif (image_res[1] > image_res[0]):
  46.         ranges = [2, 2*(image_res[1]/image_res[0])]
  47.     # equal
  48.     else:
  49.         ranges = [2, 2]
  50.     realCounter = 0
  51.     imagCounter = 0
  52.  
  53.     saveName = input("\nInput filename: ")
  54.  
  55.     image = Image.new("RGB", (image_res[0], image_res[1]))
  56.     mandelbrotImg = image.load()
  57.     sys.stdout.write("0/"+str(image_res[0])+" columns calculated")
  58.  
  59.     for real in frange(-ranges[0], ranges[0], ranges[0]*2/image_res[0]):
  60.         imagCounter = 0
  61.         sys.stdout.write("\r"+str(realCounter)+"/"+str(image_res[0])+" columns calculated")
  62.         for imag in frange(-ranges[1], ranges[1], ranges[1]*2/image_res[1]):
  63.             z = complex(0, 0j)
  64.             c = complex(real, imag)
  65.             iterations = 0
  66.             while z.real**2 + z.imag**2 < 4 and iterations < iterations_max:
  67.  
  68.                 z = z**2 + c
  69.  
  70.                 iterations += 1
  71.             if (iterations < iterations_max):
  72.                 mandelbrotImg[realCounter, imagCounter] = (int(255*(iterations/iterations_max)**(1/2.5)), 0, 0)
  73.             else:
  74.                 mandelbrotImg[realCounter, imagCounter] = (0, 0, 0)
  75.             imagCounter += 1
  76.         realCounter += 1
  77.  
  78.     image.save(saveName+".png")
  79.     image.show()
  80.  
  81. if __name__ == "__main__":
  82.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement