Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # plasma_art_demo.py
- dir=r"C:/pydemo/test/"
- from PIL import Image
- from math import pi, sin, cos
- import random
- WIDTH = 128
- HEIGHT = 128
- SIZE = WIDTH*HEIGHT
- img = Image.new("RGB", (WIDTH, HEIGHT))
- colors = [(x >> 10, (x >> 5) & 31, x & 31) for x in range(SIZE)]
- colors = [(x[0] << 3, x[1] << 3, x[2] << 3) for x in colors]
- colors.sort(key=lambda x: x[0] * 0.2126 + x[1] * 0.7152 + x[2] * 0.0722)
- def get_pixel(lum):
- for i in range(len(colors)):
- c = colors[i]
- if c[0] * 0.2126 + c[1] * 0.7152 + c[2] * 0.0722 > lum:
- break
- return colors.pop(i)
- def plasma(x, y):
- p = sin(pi * x / (32 + 10 * sin(y * pi / 32)))
- p *= cos(pi * y / 64)
- return 128 + 127 * p
- xy = []
- for x in range(WIDTH):
- for y in range(HEIGHT):
- xy.append((x, y))
- random.shuffle(xy)
- count = 0
- for x, y in xy:
- l = int(plasma(x, y))
- img.putpixel((x, y), get_pixel(plasma(x, y)))
- count += 1
- if not count & 255:
- print "%d out of %d pixels rendered" % (count,SIZE)
- img.save(dir+"plasma.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement