forked from ornotermes/Arduino-LED-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gif.py
executable file
·89 lines (70 loc) · 2.81 KB
/
gif.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
#
#+ Copyright (c) 2014 Rikard Lindstrom <ornotermes@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from serialLedLib import *
from PIL import Image
from time import sleep
import argparse
import sys
def argParser():
p = argparse.ArgumentParser()
p.add_argument( "-f", "--file", dest="file", default="file.gif", metavar="file.gif", help="Select what file to draw.")
p.add_argument( "-p", "--port", dest="port", default="/dev/ttyACM0", metavar="/dev/ttyX", help="What device to use.")
p.add_argument( "-i", "--identifier", dest="identifier", default="LED_TABLE", metavar="NAME", help="What name to look for in the identifier string.")
p.add_argument( "-d", "--debug", dest="debug", action="store_const", default=False, const=True, help="Show debug messages.")
p.add_argument( "-o", "--overlay", dest="overlay", action="store_const", default=False, const=True, help="Overlay new layer on old.")
p.add_argument( "-b", "--background", dest="back", default="FFFFFF", metavar="1F2E3D", help="Backround color.")
p.add_argument( "-r", "--reduced", dest="reduced", default="1", metavar="4", help="Divide light by this.")
return vars(p.parse_args())
def hex2rgb(s):
red = int(s[0], 16)<<4 | int(s[1], 16)
green = int(s[2], 16)<<4 | int(s[3], 16)
blue = int(s[4], 16)<<4 | int(s[5], 16)
return (red, green, blue)
if __name__ == '__main__':
arg = argParser()
back = hex2rgb( arg["back"] )
leds = serialLed();
leds.connect(arg["port"], arg["identifier"])
try:
gif = Image.open(arg["file"]);
except:
print "Can't open file."
sys.exit(1);
print (gif.info)
palette = gif.getpalette()
output = Image.new("RGBA", (leds.width, leds.height), back)
while 1:
if(1 != arg["overlay"]):
output = Image.new("RGBA", (leds.width, leds.height), back)
gif.putpalette(palette)
temp = gif.convert("RGBA")
output.paste(temp, mask=temp.split()[3])
pixdata = output.convert("RGB").load()
leds.send("C");
for y in range(0, leds.height):
for x in range(0, leds.width):
leds.send("S",map(lambda x: int(x/float(arg["reduced"])), pixdata[x,y]))
leds.send("D");
try:
sleep(gif.info["duration"] / 1000.0);
except KeyError:
sleep(0.1);
try:
gif.seek(gif.tell()+1)
except EOFError:
gif.seek(0)