farry

buddhabrot.py

Feb 13th, 2021
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 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. width, height = 900, 900   # rotated 90 degrees
  7. xmin, xmax = -1.6, 0.8
  8. itsmax = 1500
  9. qual = 5
  10.  
  11. yscale = (xmax - xmin) * height / width / 2
  12. ymin, ymax = -yscale, yscale + 0.01
  13. y, x = np.ogrid[ymin:ymax:1j*qual*height, xmin:xmax:1j*qual*width]  
  14.    
  15. c = x + 1j*y
  16. z = np.zeros_like(c)
  17. np.warnings.filterwarnings('ignore', '(overflow|invalid)')
  18. starttime = time.time()
  19.  
  20. for n in range(itsmax):
  21.     mask = (z * z.conjugate()) < 4
  22.     np.putmask(z, mask, z*z + c)
  23.  
  24. escaped = np.where(~mask)
  25. cvals = c[escaped]
  26. escvals = cvals.copy()
  27.  
  28. print("Stage1 complete:", time.time() - starttime, "seconds")
  29. starttime = time.time()
  30.  
  31. y, x = np.ogrid[ymin:ymax:1j*height, xmin:xmax:1j*width]
  32. surface = np.zeros_like(x * y, dtype=np.uint64)
  33.  
  34. for _ in range(itsmax):
  35.     newvals = escvals ** 2 + cvals
  36.     inside = np.where(abs(newvals < 10))
  37.     escvals = newvals[inside]
  38.     cvals = cvals[inside]
  39.     xpoints = np.uint64((escvals.real - xmin) * (width - 2) / (xmax - xmin))
  40.     ypoints = np.uint64((escvals.imag - ymin) * (height - 2) / (ymax - ymin))
  41.     points = ((ypoints.clip(0, height-1), xpoints.clip(0, width-1)))
  42.     surface[points] += 1
  43.    
  44. print("Stage2 complete:", time.time() - starttime, "seconds")
  45. img = Image.fromarray(np.uint8(surface.clip(0,255)).T, mode="L")
  46. img.save("buddha.png")
  47. img.show()
  48.  
Add Comment
Please, Sign In to add comment