farry

pi-rand-square.py

Mar 17th, 2020
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from mpmath import mp
  4. import numpy as np
  5. from PIL import Image
  6.  
  7. size = 256
  8. prec = size ** 2 // 4
  9. mp.dps = prec * 16 // 10 + 2
  10. intpi = int(mp.pi * 16 ** (prec - 1))
  11. tplate = '{{:0{}b}}'.format(size ** 2)
  12. binpi = np.array([tplate.format(intpi)])
  13. binarray = binpi.view("U1").astype(np.int8)
  14. greyval = (binarray * np.int8(255)).reshape(size, size)
  15. img = Image.fromarray(greyval, mode="L")
  16. img = img.resize((size * 2, size * 2))
  17. img.show()
  18. img.save("pi-square.png")
  19.  
  20. binrand = np.random.randint(2, size=size**2, dtype=np.int8)
  21. greyrand = (binrand * np.int8(255)).reshape(size, size)
  22. imgrand = Image.fromarray(greyrand, mode="L")
  23. imgrand = imgrand.resize((size * 2, size * 2))
  24. imgrand.show()
  25. imgrand.save("rand-square.png")
Advertisement
Add Comment
Please, Sign In to add comment