-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixoo64_media_album_art.py
357 lines (303 loc) · 29.4 KB
/
pixoo64_media_album_art.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
"""
Divoom Pixoo64 Album Art Display
--------------------------------
This functionality automatically showcases the album cover art of the currently playing track on your Divoom Pixoo64 screen.
In addition to this, it extracts useful information like the artist’s name and the predominant color from the album art. This extracted data can be leveraged for additional automation within your Home Assistant setup.
# appdaemon/apps/apps.yaml
-----
pixoo64_media_album_art:
module: pixoo64_media_album_art
class: Pixoo64_Media_Album_Art
home_assistant:
ha_url: "http://homeassistant.local:8123" # Home Assistant URL
media_player: "media_player.era300" # Media player entity ID
toggle: "input_boolean.pixoo64_album_art" # Boolean sensor to control script execution (Optional)
pixoo_sensor: "sensor.pixoo64_media_data" # Sensor to store media data (Optional)
light: False # RGB light entity ID (if any) (Optional)
pixoo:
url: "http://192.168.86.21:80/post" # Pixoo device URL
full_control: True # Control display on/off with play/pause
contrast: True # Apply 50% contrast filter
fail_txt: True # Show media info if image fails to load
show_text:
enabled: False # Show media info with image
text_background: True # Change background of text area
font: 2 # Pixoo internal font type (0-7)
color: False # Use alternative font color
crop_borders:
enabled: True # Crop image borders if present
extra: True # Apply enhanced border crop
"""
import re
import base64
import requests
import json
import time
import zlib
import numpy as np
import random
import traceback
from io import BytesIO
from collections import Counter
from unidecode import unidecode
from PIL import Image, UnidentifiedImageError, ImageEnhance, ImageFilter
from appdaemon.plugins.hass import hassapi as hass
NO_IMAGE = b"x\x9c\xed\xd3K\x0e\xc3 \x0c\x05\xc0#q\x86\xdc\xffR\xed\xa6RU\x81?\xd0EZ\xcdl\x92 d?\x87\xe4\xba\x00\x00\x00\x00\x00\x00\xf8W\xe3\xa9\xba>\xdetjvzD\xeb\x95\x1cc!\xab\xd5\x9d!\xab\x99\xf5\xc8\xfaV\xdfM'WT/\xaasz~\xdf\x9a\xbf\xd2\xef\xae\xf3G\xd7J\x9e\xa8_w\xcfj\x7f\xe5\xbe\xd2kw\xfe\x9d\xe7\xcf\xefh,\xccrV\xb2v\xe6\xac\xd6\x8c\xe6\x8frW\xfa\x9d\x9c\xff*\xdb]\xcf\x7f\xb6~:\xffkm7K\xb6\xb7r\xdf\xe9\xdb\xc9U\xa9\x1f\xadW\xfe\xa7,\xd3\xe9\xfcY\x8e\xd9?\xd4}'\x00\x00\x00\x00\x00\x00\x00\xbf\xe2\x01\xb1:\x16y"
TV_ICON = b'x\x9c\xed\x9a\xd9\x92\xab8\xb6\x86\x1f\x88\x0b\x06\x03\x86Ka\x063\x9aQ\x0cw\x18l\xe6\xc9\xe0\xc4\xf6\xd3\xb7\xc8\x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\x86\x1f\xa6\x7f3i\xbc\xef\x1b~+\xbf\x95%\xdf\x8f\x9fU\xd2\xbck\x96=\xc7\xa3v\xa3[}\x19e\x17\xd5\x87~c9\r\x83,B\x91\x8b?\x9b\x7f\xd3a-\x0e\x9a\xbb\xea\xfekH\x1aj\x9e*\x05\x14v\xb5\xfaFeZ\xba\xc0\x19\xff4\xad#k\xda\xe3\xff\xf6V\xe6Ws\xfa\xfb\xf1.g\x98\xcf\xd8+\x15\xf5\xd6\x1f\xd5[\xd2\x14)\xfalo\xfb\x14\xd7T\xbe\x82\xfdg\x89ksL\x1a\xd3\x0fD\xc7?8\xb1\xfa\xb7\xfc\xc8*\x89\xb6\xd7\xc0\xac!"\xed1X\x7f\xcbo\xfc\xbb*$Avh])\xd5\x93\x00\xcc\x7f\xfd.\xce\xf6N\xdfj\x8a\xdb\xfbo\x87b\xd0\xb7w\x9fD\x87\xb0\xb6\xf7$\xdf\xff\xa3\xcf\x07TH\x00\x8e\xf4^\xf6\x8f\xef@:@\x90V\xc28\n\x8e%\x14\xce\xa7\xbf\xef\xf3\xado}\xeb[\xdf\xfa\xd6\xb7\xbe\xf5\xado\xfd/\xe8\x1fi\xe3\xba\xee'
BLK_SCR = b'x\x9c\xed\xc11\x01\x00\x00\x00\xc2\xa0l\xeb_\xca\x18>@\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00o\x03\xda:@\xf1'
# ---------------
IMAGE_SIZE = 64
LOWER_PART_CROP = (3, int((IMAGE_SIZE/4)*3), IMAGE_SIZE-3, IMAGE_SIZE-3)
FULL_IMG = (0, IMAGE_SIZE, IMAGE_SIZE, IMAGE_SIZE)
HEADERS = {"Content-Type": "application/json; charset=utf-8"}
class Pixoo64_Media_Album_Art(hass.Hass):
def initialize(self):
home_assistant_args = self.args.get('home_assistant', {})
self.MEDIA_PLAYER = home_assistant_args.get("media_player", "media_player.era300")
self.TOGGLE = home_assistant_args.get("toggle", "input_boolean.pixoo64_album_art")
self.HA_URL = home_assistant_args.get("ha_url", "http://homeassistant.local:8123")
self.SENSOR = home_assistant_args.get("pixoo_sensor", "sensor.pixoo64_media_data")
self.LIGHT = home_assistant_args.get("light", None)
pixoo_args = self.args.get('pixoo', {})
self.URL = pixoo_args.get("url", "192.168.86.21:80/post")
self.FULL_CONTROL = pixoo_args.get("full_control", True)
self.FAIL_TXT = pixoo_args.get("fail_txt", True)
self.CONTRAST = pixoo_args.get("contrast", False)
show_text_args = pixoo_args.get('show_text', {})
self.SHOW_TEXT = show_text_args.get("enabled", False)
self.FONT = show_text_args.get("font", 2)
self.FONT_C = show_text_args.get("color", True)
self.TEXT_BG = show_text_args.get("text_background", True)
crop_borders_args = pixoo_args.get('crop_borders', {})
self.CROP_BORDERS = crop_borders_args.get("enabled", True)
self.CROP_EXTRA = crop_borders_args.get("extra", True)
self.FALLBACK = False
self.album_name = self.get_state(self.MEDIA_PLAYER, attribute="media_album_name")
self.listen_state(self.update_attributes, self.MEDIA_PLAYER, attribute='media_title')
self.listen_state(self.update_attributes, self.MEDIA_PLAYER)
def update_attributes(self, entity, attribute, old, new, kwargs):
try:
input_boolean = self.get_state(self.TOGGLE)
except Exception as e:
self.log(f"Error getting state for {self.TOGGLE}: {e}. Please create it in HA configuration.yaml")
self.set_state(self.TOGGLE, state="on", attributes={"friendly_name": "Pixoo64 Album Art"})
input_boolean = "on"
media_state = self.get_state(self.MEDIA_PLAYER)
if media_state in ["off", "idle", "pause"]:
self.set_state(self.SENSOR, state="off")
self.album_name = "media player is not playing - removing the album name"
if input_boolean == "on":
self.pixoo_run(media_state)
def pixoo_run(self, media_state):
payload = '{ "Command" : "Channel/GetIndex" }'
response = requests.request("POST", self.URL, headers=HEADERS, data=payload)
response_data = json.loads(response.text)
select_index = response_data.get('SelectIndex', None)
if media_state in ["playing", "on"]: # Check for playing state
title = self.get_state(self.MEDIA_PLAYER, attribute="media_title")
album_name_check = self.get_state(self.MEDIA_PLAYER, attribute="media_album_name") or title
self.send_pic = album_name_check != self.album_name
if title != "TV" and title is not None:
normalized_title = unidecode(title)
artist = self.get_state(self.MEDIA_PLAYER, attribute="media_artist")
artist = artist if artist else ""
normalized_artist = unidecode(artist) if artist else ""
picture = self.get_state(self.MEDIA_PLAYER, attribute="entity_picture")
gif_base64, font_color, recommended_font_color, brightness, background_color, background_color_rgb, recommended_font_color_rgb, most_common_color_alternative_rgb, most_common_color_alternative = self.process_picture(picture)
new_attributes = {"artist": artist,"normalized_artist": normalized_artist, "media_title": title,"normalized_title": normalized_title, "media_picture_gif_base64": gif_base64, "font_color": font_color, "font_color_alternative": recommended_font_color, "background_color_brightness": brightness, "background_color": background_color, "color_alternative_rgb": most_common_color_alternative, "background_color_rgb": background_color_rgb, "recommended_font_color_rgb": recommended_font_color_rgb, "color_alternative": most_common_color_alternative_rgb,}
self.set_state(self.SENSOR, state="on", attributes=new_attributes)
payload = {"Command":"Draw/CommandList", "CommandList":[{"Command":"Channel/OnOffScreen", "OnOff":1},{"Command": "Draw/ResetHttpGifId"},{"Command": "Draw/SendHttpGif", "PicNum": 1, "PicWidth": 64, "PicOffset": 0, "PicID": 0, "PicSpeed": 1000, "PicData": gif_base64 }]}
if self.send_pic:
self.album_name = album_name_check # Will not try to upload a new pic while listening to the same album
self.send_pixoo(payload)
if self.LIGHT:
self.control_light('on',background_color_rgb)
if self.SHOW_TEXT and not self.FALLBACK:
color_font = font_color if self.FONT_C else recommended_font_color
payload = {"Command":"Draw/SendHttpText", "TextId": 3, "x": 0, "y": 48, "dir": 0, "font": self.FONT,"TextWidth": 64, "speed": 80, "TextString": normalized_artist + " - " + normalized_title + " ", "color": color_font, "align": 1}
self.send_pixoo(payload)
if self.FALLBACK and self.FAIL_TXT:
payloads = self.create_payloads(normalized_artist, normalized_title, 13)
payload = {"Command":"Draw/CommandList", "CommandList": payloads}
self.send_pixoo(payload)
else:
payload = {"Command":"Draw/CommandList", "CommandList":[{"Command":"Channel/OnOffScreen", "OnOff":1},{"Command": "Draw/ResetHttpGifId"},{"Command": "Draw/SendHttpGif","PicNum": 1,"PicWidth": 64, "PicOffset": 0, "PicID": 0, "PicSpeed": 1000, "PicData": zlib.decompress(TV_ICON).decode() }]}
self.send_pixoo(payload)
if self.LIGHT:
self.control_light('off')
else:
self.album_name = "no music is playing and no album name"
if self.FULL_CONTROL:
payload = {"Command":"Draw/CommandList", "CommandList":[{"Command":"Draw/ClearHttpText"},{"Command": "Draw/ResetHttpGifId"},{"Command":"Channel/OnOffScreen", "OnOff":0} ]}
time.sleep(4) # Delay to not turn off the screen when choosing music tracks while playing a track
else:
payload = {"Command":"Draw/CommandList", "CommandList":[{"Command":"Draw/ClearHttpText"},{"Command": "Draw/ResetHttpGifId"},{"Command":"Channel/SetIndex", "SelectIndex": 4 },{"Command":"Channel/SetIndex", "SelectIndex": select_index }]}
media_state = self.get_state(self.MEDIA_PLAYER)
if not media_state in ["playing", "on"]:
self.send_pixoo(payload)
self.set_state(self.SENSOR, state="off")
if self.LIGHT:
self.control_light('off')
def process_picture(self, picture):
font_color = recommended_font_color = recommended_font_color_rgb = "#FFFFFF"
background_color = most_common_color_alternative_rgb = most_common_color_alternative = "#000000"
background_color_rgb = tuple(random.randint(10, 200) for _ in range(3))
brightness = 0
try:
img = self.get_image(picture)
img = self.ensure_rgb(img)
full_img = img
lower_part = img.crop(LOWER_PART_CROP)
lower_part = self.ensure_rgb(lower_part)
most_common_color = self.most_vibrant_color(lower_part)
most_common_color_alternative_rgb = self.most_vibrant_color(full_img)
most_common_color_alternative = '#%02x%02x%02x' % most_common_color_alternative_rgb
brightness = int(sum(most_common_color) / 3)
most_common_colors = [item[0] for item in Counter(lower_part.getdata()).most_common(10)]
candidate_colors = [(0, 255, 0), (255, 0, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255), (255, 0, 255)]
for color in candidate_colors:
if color not in most_common_colors:
font_color = '#%02x%02x%02x' % color
break
opposite_color = tuple(255 - i for i in most_common_color)
recommended_font_color = '#%02x%02x%02x' % opposite_color
enhancer = ImageEnhance.Contrast(full_img)
full_img = enhancer.enhance(2.0)
background_color_rgb = self.most_vibrant_color(full_img)
background_color = '#%02x%02x%02x' % most_common_color_alternative_rgb
recommended_font_color_rgb = opposite_color
if self.TEXT_BG and self.SHOW_TEXT:
lpc = (0,48,64,64)
lower_part_img = img.crop(lpc)
enhancer_lp = ImageEnhance.Brightness(lower_part_img)
normalized = brightness / 255
if normalized <= 0.5:
output_value = 0.2 + normalized * 0.6
else:
output_value = 1.5 + (normalized - 0.5) * 0.6
output_value = "{:.1f}".format(output_value)
output_value = float(output_value)
lower_part_img = enhancer_lp.enhance(output_value)
img.paste(lower_part_img, lpc)
pixels = self.get_pixels(img)
b64 = base64.b64encode(bytearray(pixels))
gif_base64 = b64.decode("utf-8")
self.FALLBACK = False
except Exception as e:
self.log(f"Error processing image. Using defualt values: {e}")
self.FALLBACK = True
if self.FAIL_TXT:
gif_base64 = zlib.decompress(BLK_SCR).decode()
else:
gif_base64 = zlib.decompress(NO_IMAGE).decode()
return gif_base64, font_color, recommended_font_color, brightness, background_color, background_color_rgb, recommended_font_color_rgb, most_common_color_alternative_rgb, most_common_color_alternative
def get_image(self, picture):
try:
response = requests.get(f"{self.HA_URL}{picture}")
img = Image.open(BytesIO(response.content))
img = img.convert("RGB")
if self.CROP_BORDERS:
temp_img = img
if self.CROP_EXTRA:
img = img.filter(ImageFilter.BoxBlur(5))
enhancer = ImageEnhance.Brightness(img)
img = enhancer.enhance(1.95)
try:
np_image = np.array(img)
edge_pixels = np.concatenate([np_image[0, :], np_image[-1, :], np_image[:, 0], np_image[:, -1]])
colors, counts = np.unique(edge_pixels, axis=0, return_counts=True)
border_color = colors[counts.argmax()]
dists = np.linalg.norm(np_image - border_color, axis=2)
mask = dists < 100 #TOLERANCE
coords = np.argwhere(mask == False)
x_min, y_min = coords.min(axis=0)
x_max, y_max = coords.max(axis=0) + 1
width, height = x_max - x_min, y_max - y_min
max_size = max(width, height)
x_center, y_center = (x_min + x_max) // 2, (y_min + y_max) // 2
x_min = max(0, x_center - max_size // 2)
y_min = max(0, y_center - max_size // 2)
x_max = min(np_image.shape[0], x_min + max_size)
y_max = min(np_image.shape[1], y_min + max_size)
if x_max - x_min < max_size:
x_min = x_max - max_size
if y_max - y_min < max_size:
y_min = y_max - max_size
img = temp_img
img = img.crop((y_min, x_min, y_max, x_max))
except Exception as e:
self.log(f"Failed to crop: {e}")
enhancer = ImageEnhance.Contrast(temp_img)
temp_img = enhancer.enhance(2.0)
img = temp_img
if self.CONTRAST:
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(1.5) # 50% contrast
img.thumbnail((IMAGE_SIZE, IMAGE_SIZE), Image.Resampling.LANCZOS)
#self.save_img(img)
return img
except UnidentifiedImageError:
self.log("Unable to identify image file.")
self.FALLBACK = True
return None
def send_pixoo(self, payload_command):
response = requests.post(self.URL, headers=HEADERS, data=json.dumps(payload_command))
if response.status_code != 200:
self.log(f"Failed to send REST: {response.content}")
def ensure_rgb(self, img):
if img.mode != "RGB":
img = img.convert("RGB")
return img
def get_pixels(self, img):
if img.mode == "RGB":
pixels = [item for p in list(img.getdata()) for item in p]
else:
pixels = list(img.getdata())
return pixels
def split_string(self, text, length):
words = text.split(' ')
lines = []
current_line = ''
for word in words:
if len(current_line) + len(word) > length:
lines.append(current_line)
current_line = word
else:
current_line += ' ' + word if current_line else word
lines.append(current_line)
return lines
def create_payloads(self, normalized_artist, normalized_title, x):
artist_lines = self.split_string(normalized_artist, x)
title_lines = self.split_string(normalized_title, x)
all_lines = artist_lines + title_lines
if len(all_lines) > 4:
all_lines[3] += ' ' + ' '.join(all_lines[4:])
all_lines = all_lines[:4]
start_y = (64 - len(all_lines) * 15) // 2
payloads = [{"Command":"Draw/ClearHttpText"}]
for i, line in enumerate(all_lines):
payload = {"Command":"Draw/SendHttpText", "TextId": i+1, "x": 0, "y": start_y + i*15, "dir": 0, "font": self.FONT, "TextWidth": 64,"speed": 80, "TextString": line, "color": "#a0e5ff" if i < len(artist_lines) else "#f9ffa0", "align": 2}
payloads.append(payload)
return payloads
def control_light(self, action, background_color_rgb=None):
service_data = {'entity_id': self.LIGHT}
if action == 'on':
service_data.update({'rgb_color': background_color_rgb, 'transition': 2 })
try:
self.call_service(f'light/turn_{action}', **service_data)
except Exception as e:
self.log(f"Light Error: {self.LIGHT} - {e}\n{traceback.format_exc()}")
def most_vibrant_color(self, full_img):
full_img.thumbnail((IMAGE_SIZE, IMAGE_SIZE), Image.Resampling.LANCZOS)
color_counts = Counter(full_img.getdata())
most_common_colors = color_counts.most_common()
for color, count in most_common_colors:
r, g, b = color
max_color = max(r, g, b)
min_color = min(r, g, b)
if max_color + min_color > 400 or max_color - min_color < 50:
continue
if max_color - min_color < 100:
continue
return color
return tuple(random.randint(100, 200) for _ in range(3))