Created
March 22, 2016 04:27
-
-
Save nnarain/984958dd05ad019621cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from PIL import Image | |
from argparse import ArgumentParser | |
parser = ArgumentParser() | |
parser.add_argument('-i', '--image', help='image') | |
args = vars(parser.parse_args()) | |
class LedRun: | |
def __init__(self): | |
self.red = 0 | |
self.green = 0 | |
self.blue = 0 | |
self.run_length = 0 | |
class LedState: | |
def __init__(self): | |
self.runs = [] | |
self.runs_length = 0 | |
class LedStrip: | |
def __init__(self, pixel_count): | |
self.pixels = [None] * pixel_count | |
def setPixel(self, idx, color): | |
self.pixels[idx] = color | |
def getState(): | |
state = LedState() | |
run_color = self.pixels[0] | |
run_length = 1 | |
for i in range(1, len(self.pixels)): | |
current_color = self.pixels[i] | |
# if the run is still continuing | |
if current_color == run_color and run_length < 255: | |
run_length += 1 | |
else: | |
# run has finished, create a new run and add the to state | |
run = LedRun() | |
run.red = self.getRed(run_color) | |
run.green = self.getGreen(run_color) | |
run.blue = self.getBlue(run_color) | |
run.run_length = run_length | |
state.runs.append(run) | |
# change to tht new run color and reset length | |
run_color = current_color | |
run_length = 1 | |
state.runs_length = len(state.runs) | |
return state | |
def getRed(self, color): | |
return color | |
def getGreen(self, color): | |
return color | |
def getBlue(self): | |
return color | |
class Animation: | |
def __init__(self, name, states): | |
self.name = name | |
self.states = state | |
pass | |
if __name__ == '__main__': | |
image_filename = args['image'] | |
image = Image.open(image_filename) | |
pixels = image.load() | |
(width, height) = image.size | |
print 'Width: %d, Height: %d' % (width, height) | |
leds = LedStrip() | |
states = [] | |
for i in range(width): | |
for j in range(height): | |
color = pixels[i,j] | |
leds.setPixel(i, color) | |
states.append(leds.getState()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment