Commit 5344ea74 authored by Christian Wander's avatar Christian Wander
Browse files

add loading of image data

parent 3e3dc003
from PIL import Image
PINK = (255, 0, 255)
LIGHTBLUE = (0, 255, 255)
BLUE = (0, 0, 255)
class Thing:
def __init__(self, matrix):
self.matrix = matrix
def __init__(self, matrix=[], file=None, width=12, height=12):
if file is None:
self.matrix = matrix
else:
self.from_png(file)
def from_png(self, file):
"""
Loads an image from a file into the object
"""
print(f'>> loading image from {file}')
img = Image.open(file)
w, h = img.size
y = 0
result = []
while y < h:
x = 0
newline = []
while x < w:
p = img.getpixel((x, y))[:3]
newline.append(p)
x += 1
result.append(newline)
y += 1
self.matrix = result
def console_print(self):
"""Prints the pixel matrix in this object to the console.
......@@ -63,14 +96,14 @@ class Thing:
return tuple(result)
def from_heart(color=PINK):
def heart_thing(color=PINK):
"""
Creates an Thing object that contains a heart
"""
return Thing(heart_tuple(color))
def from_eck(color=PINK):
def eck_thing(color=PINK):
"""
Creates a Thing object that contains an eck
"""
......@@ -125,6 +158,6 @@ if __name__ == "__main__":
# print(tuplify(eck_tuple(color)))
t = from_heart()
t = heart_thing()
print(t.console_print())
print(t.snake_tuple())
data/min.png

81 Bytes

......@@ -33,7 +33,7 @@ def main():
## sender[universe].dmx_data = blink.eck(c)
# eck = blink.tuplify(blink.snakify(blink.eck_tuple(c)))
t = blink.from_heart(c)
t = blink.heart_thing(c)
sender[universe].dmx_data = t.snake_tuple()
time.sleep(5)
......
......@@ -27,6 +27,15 @@ class TestBlink(unittest.TestCase):
self.assertEqual(t.snakify(), expected)
def test_from_png(self):
t = blink.Thing(file = 'data/min.png')
expected = [[(255, 0, 255), (255, 0, 255)],
[(0, 255, 255), (0, 255, 255)]]
self.assertEqual(t.matrix, expected)
if __name__ == "__main__":
unittest.main()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment