Advertisement
farry

mandelbrot-numpy

Oct 16th, 2021 (edited)
1,151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import numpy as np
  3. from PIL import Image # Pillow fork of PIL
  4. import time
  5.  
  6. w, h = 900, 720
  7. xc, yc, scale = - 0.7, 0.005, 1.5
  8. imax, zmax = 190, 50
  9.  
  10. t1 = time.time()
  11. y, x = np.ogrid[(yc - scale) * h/w: (yc + scale) * h/w: 1j * h,
  12.                  xc - scale: xc + scale: 1j * w ]
  13. c = x + 1j*y
  14. ones = np.ones_like(x*y)
  15. its = np.zeros_like(ones)
  16. z = c * 0
  17. np.warnings.filterwarnings('ignore', '(overflow|invalid)')
  18.  
  19. for n in range(imax):
  20.     itsmax = abs(z.imag) < zmax
  21.     np.putmask(its, itsmax, n*ones)
  22.     np.putmask(z, itsmax,  z * z + c)
  23.  
  24. backgnd = np.log1p(its + 0.1 - np.log2(np.log2(abs(z) + 1))) / 5.25
  25. np.putmask(backgnd, itsmax, 0*ones)
  26. tones = np.uint8(backgnd * 255)
  27. blues = np.vstack(np.linspace(0.2, 1, 160)) ** (4, 2.5, 1)
  28. sepias = np.vstack(np.linspace(1, 0, 96)) ** (1, 1.5, 3)
  29. rgbcolors = np.uint8(np.vstack((blues, sepias)) * 255)
  30. img = Image.fromarray(rgbcolors[tones], mode="RGB")
  31. img.save("mand.png")
  32. print("Elapsed time: {:7.2f} s".format(time.time() - t1))
  33. img.show()
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement