-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout.py
174 lines (143 loc) · 5.09 KB
/
layout.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
Subway arrival sign layout
"""
import config
import time
import csv
import json
import math
from PIL import Image, ImageDraw, ImageFont, ImageColor
LINE_HEIGHT = 16
DISPLAY_HEIGHT = config.DISPLAY_HEIGHT
DISPLAY_WIDTH = config.DISPLAY_WIDTH
line_heights = [LINE_HEIGHT / 2, DISPLAY_HEIGHT - LINE_HEIGHT / 2 + 1]
BULLET_SIZE = 14
ORDER_WIDTH = 8
with open('routes.txt', 'r') as f:
routes = {r['route_id']: r for r in csv.DictReader(f)}
with open('stops.txt', 'r') as f:
stops = {s['stop_id']: s for s in csv.DictReader(f)}
pokemon = ImageFont.truetype('Pokemon X and Y.ttf', 11)
fifteen = ImageFont.truetype('15x5.ttf', 16)
pm = ImageFont.truetype('pixelmix_bold.ttf', 8)
def new_frame():
return Image.new('RGB', (DISPLAY_WIDTH, DISPLAY_HEIGHT))
def splash_screen():
img = new_frame()
draw = ImageDraw.Draw(img)
draw.text((0, 0), text=f"signpi 0.3 mode={config.mode}", anchor='lt', font=pokemon)
draw.text((DISPLAY_WIDTH, 0), text=time.strftime('%Y-%m-%d %H:%M'), anchor='rt', font=pokemon)
if config.mode == 'subway':
draw.text((0, DISPLAY_HEIGHT), text=stops[config.subway.get('station')]['stop_name'], anchor='lb', font=pokemon)
draw.text((DISPLAY_WIDTH, DISPLAY_HEIGHT), text=config.subway.get('direction'), anchor='rb', font=pokemon)
return [img], [1]
def subway():
with open('trips.json') as f:
trips = json.load(f)
now = time.time()
if len(trips) < 2 or max(t['timestamp'] for t in trips) < now - 60 * 5:
img = new_frame()
draw = ImageDraw.Draw(img)
draw.text((0, 0), "No data", anchor='lt')
#TODO: better no data screen
return [img], 1
frames = []
frame_times = []
for page in range(min(config.subway.getint('pages', 2), len(trips) // 2)):
img = new_frame()
for i in range(page * 2, (page + 1) * 2):
stop_id = trips[i]['destination_stop']
eta_min = round(
(trips[i]['estimated_current_stop_arrival_time'] - now) / 60)
if eta_min > 99 or trips[i]['is_delayed']:
eta = 'Delay'
else:
eta = f'{eta_min} min'
draw_trip(img,
line_heights[i % len(line_heights)],
order=f'{i+1}.',
route=trips[i]['route_id'],
dest=stops[stop_id]['stop_name'],
dest_short='x',
eta=eta)
frames.append(img)
frame_times.append(10)
return frames, frame_times
def draw_trip(img,
y,
order,
route,
dest,
dest_short,
eta,
eta_fill=(255, 255, 255)):
order_font = fifteen
main_font = pokemon
BULLET_SIZE = 14
ORDER_WIDTH = 8
draw = ImageDraw.Draw(img)
draw.fontmode = '1' # no antialiasing
draw.text((0, y),
order,
font=order_font,
anchor='lm',
fill=(255, 255, 255))
draw_bullet(img, route, (ORDER_WIDTH + BULLET_SIZE - BULLET_SIZE // 2, y-1), BULLET_SIZE)
dest_x = ORDER_WIDTH + BULLET_SIZE + 4
available_space = DISPLAY_WIDTH - dest_x - max(
main_font.getlength('Delay'), main_font.getlength('99 min'))
draw.text((dest_x, y),
text=dest
if main_font.getlength(dest) < available_space else dest_short,
font=main_font,
anchor='lm',
fill=(255, 255, 255))
draw.text((DISPLAY_WIDTH, y),
text=eta,
anchor="rm",
font=main_font,
fill=eta_fill)
def draw_bullet(img, route_id, center, diameter):
draw = ImageDraw.Draw(img)
draw.fontmode = '1'
bullet_font = pm
x, y = center
route = routes[route_id]
if len(route['route_color']) == 6:
route_color = ImageColor.getrgb(f"#{route['route_color']}")
else:
if route_id in ('GS', 'FS', 'H'):
route_color = ImageColor.getrgb('#6D6E71')
else:
route_color = ImageColor.getrgb('black')
if len(route['route_text_color']) == 6:
route_text_color = ImageColor.getrgb(
f"#{route['route_text_color']}")
else:
if route_id in ('N', 'Q', 'R', 'W'):
route_text_color = ImageColor.getrgb('black')
else:
route_text_color = ImageColor.getrgb('white')
bounds = [[x - math.floor(diameter / 2), x + math.ceil(diameter / 2)],
[y - math.floor(diameter / 2), y + math.ceil(diameter / 2)]]
if route_id in ('FX', '5X', '6X', '7X'):
# draw diamond
route_text = route_id[:1]
draw.polygon([
(bounds[0][0]-1, y),
(x, bounds[1][0]-1 ),
(bounds[0][1]+1, y),
(x, bounds[1][1]+1)],
fill=route_color)
else:
route_text = route['route_short_name']
draw.ellipse([(bounds[0][0], bounds[1][0]),
(bounds[0][1], bounds[1][1])],
fill=route_color)
draw.text(
(x+1, y),
route_text,
font=bullet_font,
anchor='mm',
fill=route_text_color
)