-
Notifications
You must be signed in to change notification settings - Fork 1
/
play.py
executable file
·58 lines (46 loc) · 1.5 KB
/
play.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
#!/usr/bin/env -S python3 -u
from PIL import Image
import sys
import time
def print8(image_region, ymin, ymax):
width, height = image_region.size
assert ymax - ymin == 7
assert 0 <= ymin <= height
assert 0 <= ymax <= height
for column in range(width-1, -1, -1):
# Build a single byte (display column) from 8 pixels:
tmp = 0
for row in range(height):
if ymin <= row <= ymax:
pixel = image.getpixel((column, row))
if pixel in [ 0, (0,0,0), (0,0,0,255)]:
tmp |= 1 << (row - ymin)
# escape the control-character by converting '.' -> '..'
if tmp == ord('.'):
sys.stdout.buffer.write(b'..')
else:
# Write the raw byte
sys.stdout.buffer.write(tmp.to_bytes(1, 'big'))
# Strobe the shift register contents to display
sys.stdout.buffer.write(b'.s')
def display(image):
_, height = image.size
if height == 8:
print8(image, 0, 7)
elif height == 16:
sys.stdout.buffer.write(b'.0')
print8(image, 0, 7)
sys.stdout.buffer.write(b'.1')
print8(image, 8, 15)
# Read a 72x8 or 72x16 image
image = Image.open(sys.argv[1])
# Quiet mode, e.g. do not block trying to write response message to USB
sys.stdout.buffer.write(b'.q')
if image.is_animated:
while True:
for frame in range(image.n_frames):
image.seek(frame)
display(image)
time.sleep(0.1)
else:
display(image)