Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import time
- import random
- import math
- import copy
- from PIL import Image, ImageDraw
- from sense_hat import SenseHat
- sense = SenseHat()
- pixels = [
- [255, 0, 0], [255, 0, 0], [255, 87, 0], [255, 196, 0], [205, 255, 0], [95, 255, 0], [0, 255, 13], [0, 255, 122],
- [255, 0, 0], [255, 96, 0], [255, 205, 0], [196, 255, 0], [87, 255, 0], [0, 255, 22], [0, 255, 131], [0, 255, 240],
- [255, 105, 0], [255, 214, 0], [187, 255, 0], [78, 255, 0], [0, 255, 30], [0, 255, 140], [0, 255, 248], [0, 152, 255],
- [255, 223, 0], [178, 255, 0], [70, 255, 0], [0, 255, 40], [0, 255, 148], [0, 253, 255], [0, 144, 255], [0, 34, 255],
- [170, 255, 0], [61, 255, 0], [0, 255, 48], [0, 255, 157], [0, 243, 255], [0, 134, 255], [0, 26, 255], [83, 0, 255],
- [52, 255, 0], [0, 255, 57], [0, 255, 166], [0, 235, 255], [0, 126, 255], [0, 17, 255], [92, 0, 255], [201, 0, 255],
- [0, 255, 66], [0, 255, 174], [0, 226, 255], [0, 117, 255], [0, 8, 255], [100, 0, 255], [210, 0, 255], [255, 0, 192],
- [0, 255, 183], [0, 217, 255], [0, 109, 255], [0, 0, 255], [110, 0, 255], [218, 0, 255], [255, 0, 183], [255, 0, 74]
- ]
- oldpixels = copy.copy(pixels)
- msleep = lambda x: time.sleep(x / 1000.0)
- def generateframe(rgb,persistence):
- imgx = 8; imgy = 8 # image size
- image = Image.new("RGB", (imgx, imgy))
- draw = ImageDraw.Draw(image)
- octaves = int(math.log(max(imgx, imgy), 2.0))
- imgAr = [[0.0 for i in range(imgx)] for j in range(imgy)] # image array
- totAmp = 0.0
- for k in range(octaves):
- freq = 2 ** k
- amp = persistence ** k
- totAmp += amp
- # create an image from n by m grid of random numbers (w/ amplitude)
- # using Bilinear Interpolation
- n = freq + 1; m = freq + 1 # grid size
- ar = [[random.random() * amp for i in range(n)] for j in range(m)]
- nx = imgx / (n - 1.0); ny = imgy / (m - 1.0)
- for ky in range(imgy):
- for kx in range(imgx):
- i = int(kx / nx); j = int(ky / ny)
- dx0 = kx - i * nx; dx1 = nx - dx0
- dy0 = ky - j * ny; dy1 = ny - dy0
- z = ar[j][i] * dx1 * dy1
- z += ar[j][i + 1] * dx0 * dy1
- z += ar[j + 1][i] * dx1 * dy0
- z += ar[j + 1][i + 1] * dx0 * dy0
- z /= nx * ny
- imgAr[ky][kx] += z # add image layers together
- # paint image
- for ky in range(imgy):
- for kx in range(imgx):
- c = int(imgAr[ky][kx] / totAmp * 255)
- if (rgb == 'r'):
- pixels[kx + (ky * 8)] = (c,0,0)
- if (rgb == 'g'):
- pixels[kx + (ky * 8)] = ( pixels[kx + (ky * 8)][0],c,0)
- if (rgb == 'b'):
- pixels[kx + (ky * 8)] = ( pixels[kx + (ky * 8)][0], pixels[kx + (ky * 8)][1],c)
- def movetopixels():
- somethingchanged = 0
- i = 0
- for i in range(len(oldpixels)):
- if (oldpixels[i][0] > pixels[i][0]):
- oldpixels[i][0] -= 1
- somethingchanged = 1
- if (oldpixels[i][0] < pixels[i][0]):
- oldpixels[i][0] += 1
- somethingchanged = 1
- if (oldpixels[i][1] > pixels[i][1]):
- oldpixels[i][1] -= 1
- somethingchanged = 1
- if (oldpixels[i][1] < pixels[i][1]):
- oldpixels[i][1] += 1
- somethingchanged = 1
- if (oldpixels[i][2] > pixels[i][2]):
- oldpixels[i][2] -= 1
- somethingchanged = 1
- if (oldpixels[i][2] < pixels[i][2]):
- oldpixels[i][2] += 1
- somethingchanged = 1
- return somethingchanged
- persistence_r = random.random()
- persistence_g = random.random()
- persistence_b = random.random()
- while True:
- generateframe("r",persistence_r)
- generateframe("g",persistence_g)
- generateframe("b",persistence_b)
- while movetopixels() == 1:
- sense.set_pixels(oldpixels)
- msleep(4)
- msleep(8)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement