-
Notifications
You must be signed in to change notification settings - Fork 0
/
ritooled.py
73 lines (64 loc) · 1.93 KB
/
ritooled.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
from PIL import Image, ImageSequence
import os
import sys
import numpy
import getopt
import rivaloled
VENDOR_ID = 0x1038
PRODUCT_ID = 0x1700
MAXX, MAXY = 36, 128
def isanimation(im):
try:
im.seek(1)
except EOFError:
return False
else:
return True
def processandsend(im, delay):
im = im.convert('1', dither=0)
frame = rivaloled.imagetobits(im)
rivaloled.sendframe(frame, delay)
def main(argv):
filename = ""
timeout = 300000
try:
opts, args = getopt.getopt(sys.argv[1:], "hi:d:")
except getopt.GetoptError as err:
print("ritooled.py -i <image> -d <bool>")
sys.exit(2)
for opt, arg in opts:
if opt == "-h":
print("ritooled.py -i <image> -d <bool")
sys.exit()
elif opt in "-i":
filename = arg
elif opt in "-d":
d = int(arg)
if d >= 1500 and d <= 600000:
timeout = int(arg)
else:
print("delay must be greater than 1500 and less than 600000")
if not os.path.exists(filename):
raise Exception("File: %s does not exist." % filename)
try:
im = Image.open(filename, "r")
y, x = im.size
imf = im.format
if imf != "GIF":
raise Exception("File: %s needs to be in gif format." % filename)
if y > MAXY or x > MAXX:
raise Exception("Image must be 128x36 pixel")
if not isanimation(im):
processandsend(im, timeout)
else:
seq = []
for frame in ImageSequence.Iterator(im):
im = frame.convert('1', dither=0)
part = rivaloled.imagetobits(im)
seq.append(part)
frames = numpy.dstack(seq)
rivaloled.sendsquenece(frames, timeout)
except IOError:
raise Exception("File: %s is not a valid image." % filename)
if __name__ == "__main__":
main(sys.argv[1:])