-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_image.py
executable file
·75 lines (52 loc) · 1.68 KB
/
play_image.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
#!/usr/bin/env python3
"""
Play a png file line by line
"""
import time
import socket
import argparse
from PIL import Image
from treppe import protocol
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-H", "--host", default="localhost")
parser.add_argument("-c", "--crap", default=False, action="store_true")
parser.add_argument("-p", "--port", type=int, default=3123)
parser.add_argument("-f", "--fps", type=int, default=30)
parser.add_argument("-l", "--leds", type=int, default=16)
parser.add_argument("filename", nargs=1)
return parser.parse_args()
def _decode_rgba(rgba):
return (rgba[0] / 255.0,
rgba[1] / 255.0,
rgba[2] / 255.0,
1.0 - (rgba[3] / 255.0))
def _get_col(image, row, max_width):
data = []
for i in range(0, max_width):
try:
data.append(_decode_rgba(image.getpixel((row, i))))
except IndexError:
data.append((0,0,0,0))
return data
def play_image(conn, filename, fps, crap, leds):
image = Image.open(filename)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
for x in range(image.width):
frame = _get_col(image, x, leds)
if crap:
sock.sendto(protocol.encode_frame_crap8(frame), conn)
else:
sock.sendto(protocol.cmd_frame_rgbw16(frame), conn)
time.sleep(1.0 / fps)
def main(args):
conn = (args.host, args.port)
play_image(conn,
args.filename[0],
args.fps,
args.crap,
args.leds)
if __name__ == "__main__":
args = parse_args()
main(args)