Advertisement
here2share

# plasma_art_demo.py

Jun 20th, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. # plasma_art_demo.py
  2.  
  3. dir=r"C:/pydemo/test/"
  4.  
  5. from PIL import Image
  6. from math import pi, sin, cos
  7. import random
  8.  
  9. WIDTH = 128
  10. HEIGHT = 128
  11. SIZE = WIDTH*HEIGHT
  12.  
  13. img = Image.new("RGB", (WIDTH, HEIGHT))
  14.  
  15. colors = [(x >> 10, (x >> 5) & 31, x & 31) for x in range(SIZE)]
  16. colors = [(x[0] << 3, x[1] << 3, x[2] << 3) for x in colors]
  17. colors.sort(key=lambda x: x[0] * 0.2126 + x[1] * 0.7152 + x[2] * 0.0722)
  18.  
  19. def get_pixel(lum):
  20.     for i in range(len(colors)):
  21.         c = colors[i]
  22.         if c[0] * 0.2126 + c[1] * 0.7152 + c[2] * 0.0722 > lum:
  23.             break
  24.     return colors.pop(i)
  25.  
  26. def plasma(x, y):
  27.     p = sin(pi * x / (32 + 10 * sin(y * pi / 32)))
  28.     p *= cos(pi * y / 64)
  29.     return 128 + 127 * p
  30.  
  31. xy = []
  32. for x in range(WIDTH):
  33.     for y in range(HEIGHT):
  34.         xy.append((x, y))
  35. random.shuffle(xy)
  36.  
  37. count = 0
  38. for x, y in xy:
  39.     l = int(plasma(x, y))
  40.     img.putpixel((x, y), get_pixel(plasma(x, y)))
  41.     count += 1
  42.     if not count & 255:
  43.         print "%d out of %d pixels rendered" % (count,SIZE)
  44.  
  45. img.save(dir+"plasma.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement