Advertisement
Guest User

plasma.py

a guest
Jan 10th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. #!/usr/bin/python
  2. import time
  3. import random
  4. import math
  5. import copy
  6. from PIL import Image, ImageDraw
  7. from sense_hat import SenseHat
  8.  
  9. sense = SenseHat()
  10.  
  11. pixels = [
  12.     [255, 0, 0], [255, 0, 0], [255, 87, 0], [255, 196, 0], [205, 255, 0], [95, 255, 0], [0, 255, 13], [0, 255, 122],
  13.     [255, 0, 0], [255, 96, 0], [255, 205, 0], [196, 255, 0], [87, 255, 0], [0, 255, 22], [0, 255, 131], [0, 255, 240],
  14.     [255, 105, 0], [255, 214, 0], [187, 255, 0], [78, 255, 0], [0, 255, 30], [0, 255, 140], [0, 255, 248], [0, 152, 255],
  15.     [255, 223, 0], [178, 255, 0], [70, 255, 0], [0, 255, 40], [0, 255, 148], [0, 253, 255], [0, 144, 255], [0, 34, 255],
  16.     [170, 255, 0], [61, 255, 0], [0, 255, 48], [0, 255, 157], [0, 243, 255], [0, 134, 255], [0, 26, 255], [83, 0, 255],
  17.     [52, 255, 0], [0, 255, 57], [0, 255, 166], [0, 235, 255], [0, 126, 255], [0, 17, 255], [92, 0, 255], [201, 0, 255],
  18.     [0, 255, 66], [0, 255, 174], [0, 226, 255], [0, 117, 255], [0, 8, 255], [100, 0, 255], [210, 0, 255], [255, 0, 192],
  19.     [0, 255, 183], [0, 217, 255], [0, 109, 255], [0, 0, 255], [110, 0, 255], [218, 0, 255], [255, 0, 183], [255, 0, 74]
  20. ]
  21.  
  22. oldpixels = copy.copy(pixels)
  23.  
  24. msleep = lambda x: time.sleep(x / 1000.0)
  25.  
  26. def generateframe(rgb,persistence):
  27.     imgx = 8; imgy = 8 # image size
  28.     image = Image.new("RGB", (imgx, imgy))
  29.     draw = ImageDraw.Draw(image)
  30.     octaves = int(math.log(max(imgx, imgy), 2.0))
  31.     imgAr = [[0.0 for i in range(imgx)] for j in range(imgy)] # image array
  32.     totAmp = 0.0
  33.     for k in range(octaves):
  34.         freq = 2 ** k
  35.         amp = persistence ** k
  36.         totAmp += amp
  37.         # create an image from n by m grid of random numbers (w/ amplitude)
  38.         # using Bilinear Interpolation
  39.         n = freq + 1; m = freq + 1 # grid size
  40.         ar = [[random.random() * amp for i in range(n)] for j in range(m)]
  41.         nx = imgx / (n - 1.0); ny = imgy / (m - 1.0)
  42.         for ky in range(imgy):
  43.             for kx in range(imgx):
  44.                 i = int(kx / nx); j = int(ky / ny)
  45.                 dx0 = kx - i * nx; dx1 = nx - dx0
  46.                 dy0 = ky - j * ny; dy1 = ny - dy0
  47.                 z = ar[j][i] * dx1 * dy1
  48.                 z += ar[j][i + 1] * dx0 * dy1
  49.                 z += ar[j + 1][i] * dx1 * dy0
  50.                 z += ar[j + 1][i + 1] * dx0 * dy0
  51.                 z /= nx * ny
  52.                 imgAr[ky][kx] += z # add image layers together
  53.  
  54.     # paint image
  55.     for ky in range(imgy):
  56.         for kx in range(imgx):
  57.             c = int(imgAr[ky][kx] / totAmp * 255)
  58.         if (rgb == 'r'):
  59.                  pixels[kx + (ky * 8)] = (c,0,0)
  60.             if (rgb == 'g'):
  61.                  pixels[kx + (ky * 8)] = ( pixels[kx + (ky * 8)][0],c,0)
  62.             if (rgb == 'b'):
  63.                  pixels[kx + (ky * 8)] = ( pixels[kx + (ky * 8)][0], pixels[kx + (ky * 8)][1],c)
  64.  
  65. def movetopixels():
  66.     somethingchanged = 0
  67.     i = 0
  68.     for i in range(len(oldpixels)):
  69.        if (oldpixels[i][0] > pixels[i][0]):
  70.            oldpixels[i][0] -= 1
  71.            somethingchanged = 1
  72.        if (oldpixels[i][0] < pixels[i][0]):
  73.            oldpixels[i][0] += 1
  74.            somethingchanged = 1
  75.        if (oldpixels[i][1] > pixels[i][1]):
  76.            oldpixels[i][1] -= 1
  77.            somethingchanged = 1
  78.        if (oldpixels[i][1] < pixels[i][1]):
  79.            oldpixels[i][1] += 1
  80.            somethingchanged = 1
  81.        if (oldpixels[i][2] > pixels[i][2]):
  82.            oldpixels[i][2] -= 1
  83.            somethingchanged = 1
  84.        if (oldpixels[i][2] < pixels[i][2]):
  85.            oldpixels[i][2] += 1
  86.            somethingchanged = 1
  87.     return somethingchanged
  88.  
  89.  
  90. persistence_r = random.random()
  91. persistence_g = random.random()
  92. persistence_b = random.random()
  93. while True:
  94.  
  95.     generateframe("r",persistence_r)
  96.     generateframe("g",persistence_g)
  97.     generateframe("b",persistence_b)
  98.  
  99.     while movetopixels() == 1:
  100.         sense.set_pixels(oldpixels)
  101.         msleep(4)
  102.  
  103.     msleep(8)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement