Guest User

Untitled

a guest
Apr 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. from PIL import Image
  2. import random
  3.  
  4.  
  5. def thing(val):
  6. val += (-4, -3, -2, -1, 1, 2, 3, 4)[random.getrandbits(3)]
  7. if val < 0: return 0
  8. if val > 255: return 255
  9. return val
  10.  
  11.  
  12. def neighbours(coord, w, h):
  13. offsets = ((-1, -1), (-1, 0), (-1, 1), (1, -1), (1, 0), (1, 1), (0, -1), (0, 1))
  14. for xo, yo in offsets:
  15. nx = coord[0] + xo
  16. ny = coord[1] + yo
  17. if nx >= 0 and nx < w and ny >= 0 and ny < h:
  18. yield nx, ny
  19.  
  20.  
  21. w = 720
  22. h = 1280
  23. im = Image.new('RGB', (w, h))
  24. save_frames = False
  25.  
  26. done = set()
  27. total = w * h
  28. pending = set()
  29. pending.add((0, 0, 0, (0, 0)))
  30.  
  31. n_iters = 0
  32. frame = 0
  33. while pending:
  34. if n_iters % 10000 == 0:
  35. print(len(done), total, frame)
  36. if save_frames:
  37. im.save(f'{frame:04d}.bmp')
  38. frame += 1
  39.  
  40. r, g, b, dest = pending.pop()
  41.  
  42. r = thing(r)
  43. g = thing(g)
  44. b = thing(b)
  45.  
  46. im.putpixel(dest, (r, g, b))
  47. done.add(dest)
  48. n_iters += 1
  49.  
  50. for neighbour in neighbours(dest, w, h):
  51. if neighbour not in done:
  52. pending.add((r, g, b, neighbour))
  53.  
  54. if save_frames:
  55. im.save(f'{frame:04d}.bmp')
  56. else:
  57. im.save('ass.png')
Add Comment
Please, Sign In to add comment