-
Notifications
You must be signed in to change notification settings - Fork 8
/
__init__.py
252 lines (187 loc) · 8.98 KB
/
__init__.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"""pidi plugin for Tk display."""
import time
import math
import os
try:
from pidi.display import Display
except ImportError:
from mopidy_pidi.plugin import Display
__version__ = '0.1.0'
def text_in_rect(canvas, text, font, rect, line_spacing=1.1):
width = rect[2] - rect[0]
height = rect[3] - rect[1]
# Given a rectangle, reflow and scale text to fit, centred
while font.size > 0:
space_width = font.getsize(" ")[0]
line_height = int(font.size * line_spacing)
max_lines = math.floor(height / line_height)
lines = []
# Determine if text can fit at current scale.
words = text.split(" ")
while len(lines) < max_lines and len(words) > 0:
line = []
while len(words) > 0 and font.getsize(" ".join(line + [words[0]]))[0] <= width:
line.append(words.pop(0))
lines.append(" ".join(line))
if(len(lines)) <= max_lines and len(words) == 0:
# Solution is found, render the text.
y = int(rect[1] + (height / 2) - (len(lines) * line_height / 2) - (line_height - font.size) / 2)
bounds = [rect[2], y, rect[0], y + len(lines) * line_height]
for line in lines:
line_width = font.getsize(line)[0]
x = int(rect[0] + (width / 2) - (line_width / 2))
bounds[0] = min(bounds[0], x)
bounds[2] = max(bounds[2], x + line_width)
canvas.text((x, y), line, font=font)
y += line_height
return tuple(bounds)
font = ImageFont.truetype(font.path, font.size - 1)
def draw_progress_bar(canvas, progress, max_progress, rect, colour):
unfilled_opacity = 0.5 # Factor to scale down colour/opacity of unfilled bar.
# Calculate bar widths.
rect = tuple(rect) # Space which bar occupies.
full_width = rect[2] - rect[0]
bar_width = int((progress / max_progress) * full_width)
progress_rect = (rect[0], rect[1], rect[0] + bar_width, rect[3])
# Knock back unfilled part of bar.
unfilled_colour = tuple(int(c * unfilled_opacity) for c in colour)
# Draw bars.
canvas.rectangle(rect, unfilled_colour)
canvas.rectangle(progress_rect, colour)
class DisplayPIL(Display):
"""Base class for PIL-based image displays."""
def __init__(self, args=None):
global Image, ImageDraw, ImageFilter, ImageFont, ConnectionIII
Display.__init__(self, args)
from fonts.ttf import RobotoMedium as UserFont
from PIL import Image, ImageDraw, ImageFilter, ImageFont
self._downscale = 2
self._font = ImageFont.truetype(UserFont, 42 * self._downscale)
self._font_small = ImageFont.truetype(UserFont, 20 * self._downscale)
self._font_medium = ImageFont.truetype(UserFont, 25 * self._downscale)
source_size = (self._size * self._downscale, self._size * self._downscale)
target_size = (self._size, self._size)
self._image_album_art = Image.new('RGBA', target_size , (0, 0, 0))
self._image_album_art_prev = Image.new('RGBA', target_size, (0, 0, 0))
self._text = Image.new('RGBA', source_size)
self._text_draw = ImageDraw.Draw(self._text, 'RGBA')
self._text_draw.fontmode = '1'
self._text_1x = None
self._overlay = Image.new('RGBA', target_size)
self._overlay_draw = ImageDraw.Draw(self._overlay, 'RGBA')
self._output_image = None
self._last_art_change = time.time()
self._blur = args.blur_album_art
self._image_dir = os.path.join(os.path.dirname(__file__), "images")
self.controls_pause = Image.open(
os.path.join(self._image_dir, "controls-pause.png")).resize(target_size, resample=Image.LANCZOS).convert("RGBA")
self.controls_play = Image.open(
os.path.join(self._image_dir, "controls-play.png")).resize(target_size, resample=Image.LANCZOS).convert("RGBA")
self._last_artist = ""
self._last_title = ""
self._last_album = ""
self._last_state = ""
self._last_volume = -1
self._last_progress = -1
def update_album_art(self, input_file):
# Save the old album art
self._image_album_art_prev.paste(self._image_album_art, (0, 0))
# Prepare the new album art
new = Image.open(input_file).resize((self._size * self._downscale, self._size * self._downscale))
if self._blur:
new = new.convert('RGBA').filter(ImageFilter.GaussianBlur(radius=5*self._downscale))
new = new.resize((self._size, self._size), resample=Image.LANCZOS)
self._image_album_art.paste(new, (0, 0))
self._last_art_change = time.time()
def update_overlay(self, *args, **kwargs):
Display.update_overlay(self, *args, **kwargs)
if (self._last_title, self._last_artist, self._last_album) != (self._title, self._artist, self._album):
self.update_text_layer()
self._last_title = self._title
self._last_artist = self._artist
self._last_album = self._album
def update_text_layer(self):
self._text_draw.rectangle((0, 0, self._size * self._downscale, self._size * self._downscale), (0, 0, 0, 0))
margin = 5
width = self._size * self._downscale
# Artist
artist = self._artist
if ";" in artist:
artist = artist.replace(";", ", ") # Swap out weird semicolons for commas
box = text_in_rect(self._text_draw, artist, self._font_medium, (margin, 5 * self._downscale, width - margin, 35 * self._downscale))
# Album
text_in_rect(self._text_draw, self._album, self._font_small, (50 * self._downscale, box[3], width - (50 * self._downscale), 70 * self._downscale))
# Song title
text_in_rect(self._text_draw, self._title, self._font, (margin, 95 * self._downscale, width - margin, 170 * self._downscale))
# Downscale track information
self._text_1x = self._text.resize((int(width / self._downscale), int(width / self._downscale)), resample=Image.LANCZOS)
def redraw(self):
margin = 5
t_blend = min(0.25, time.time() - self._last_art_change)
t_blend *= 4
# We don't want to update on every millisecond of progress,
# only when it happens to cross to the next pixel on the display
# and trigger a visual change
progress_pixels = int(self._progress * (self._size - margin - margin))
if self._output_image is not None \
and t_blend == 1.0 \
and self._state == self._last_state \
and self._volume == self._last_volume \
and self._last_progress == progress_pixels:
return False # No change since last frame
self._last_state = self._state
self._last_volume = self._volume
self._last_progress = progress_pixels
self._last_redraw = time.time()
# Initial setup
self._overlay_draw.rectangle((0, 0, self._size, self._size), (0, 0, 0, 40))
# Song progress bar
progress = self._progress
max_progress = 1.0
colour = (225, 225, 225, 225)
rect = (5, 220, 235, 235)
draw_progress_bar(self._overlay_draw, progress, max_progress, rect, colour)
# Volume bar
volume = self._volume
max_volume = 100
colour = (225, 225, 225, 165)
rect = (5, 185, 205, 190)
draw_progress_bar(self._overlay_draw, volume, max_volume, rect, colour)
# Crossfade Album Art
if t_blend == 1.0:
art = self._image_album_art
else:
art = Image.blend(self._image_album_art_prev, self._image_album_art, t_blend)
# Add our text layer
if self._text_1x is not None:
overlay = Image.alpha_composite(self._overlay, self._text_1x)
else:
overlay = self._overlay
# Overlay control icons
if self._state == "play":
overlay = Image.alpha_composite(overlay, self.controls_pause)
else:
overlay = Image.alpha_composite(overlay, self.controls_play)
# Overlay combined track info onto album art
image = Image.alpha_composite(art, overlay)
self._output_image = image
return True
def add_args(argparse):
Display.add_args(argparse)
argparse.add_argument("--blur-album-art",
help="Apply blur effect to album art.",
action='store_true')
class DisplayFile(DisplayPIL):
option_name = 'file'
def __init__(self, args=None):
DisplayPIL.__init__(self, args)
self._output_file = args.output_file
def add_args(argparse):
DisplayPIL.add_args(argparse)
argparse.add_argument("--output-file",
help="File to output display image.",
type=str,
default="pidi-output.png")
def redraw(self):
DisplayPIL.redraw(self)
self._output_image.save(self._output_file, "PNG")