Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cmath
- import math
- import time
- import numpy as np
- from PIL import Image
- import sys
- def frange(start, end=None, inc=None):
- # A range function, that does accept float increments...
- if end is None:
- end = start + 0.0
- start = 0.0
- if inc is None:
- inc = 1.0
- L = []
- while 1:
- next = start + len(L) * inc
- if inc > 0 and next >= end:
- break
- elif inc < 0 and next <= end:
- break
- L.append(next)
- return L
- def main():
- c = complex(0, 0j)
- z = complex(0, 0j)
- iterations = 0
- iterations_max = 1000
- image_res = [int(input("Horz. Res: ")), int(input("Vert. Res: "))]
- ranges = [0, 0]
- # image is long
- if (image_res[0] > image_res[1]):
- ranges = [2*(image_res[0]/image_res[1]), 2]
- # image is tall
- elif (image_res[1] > image_res[0]):
- ranges = [2, 2*(image_res[1]/image_res[0])]
- # equal
- else:
- ranges = [2, 2]
- realCounter = 0
- imagCounter = 0
- saveName = input("\nInput filename: ")
- image = Image.new("RGB", (image_res[0], image_res[1]))
- mandelbrotImg = image.load()
- sys.stdout.write("0/"+str(image_res[0])+" columns calculated")
- for real in frange(-ranges[0], ranges[0], ranges[0]*2/image_res[0]):
- imagCounter = 0
- sys.stdout.write("\r"+str(realCounter)+"/"+str(image_res[0])+" columns calculated")
- for imag in frange(-ranges[1], ranges[1], ranges[1]*2/image_res[1]):
- z = complex(0, 0j)
- c = complex(real, imag)
- iterations = 0
- while z.real**2 + z.imag**2 < 4 and iterations < iterations_max:
- z = z**2 + c
- iterations += 1
- if (iterations < iterations_max):
- mandelbrotImg[realCounter, imagCounter] = (int(255*(iterations/iterations_max)**(1/2.5)), 0, 0)
- else:
- mandelbrotImg[realCounter, imagCounter] = (0, 0, 0)
- imagCounter += 1
- realCounter += 1
- image.save(saveName+".png")
- image.show()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement