From 69a3db56accc2b09f37880116a500cd13d9348e7 Mon Sep 17 00:00:00 2001 From: Sundog Date: Tue, 21 Jul 2020 12:52:00 -0700 Subject: [PATCH] make album art fit inside overlay, use top left pixel of album art as overlay color, only change album art on album changes --- SoundslabDisplay.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/SoundslabDisplay.py b/SoundslabDisplay.py index a181749..3621df5 100644 --- a/SoundslabDisplay.py +++ b/SoundslabDisplay.py @@ -24,10 +24,14 @@ class SoundslabDisplay: self.current_overlay = Image.new("RGBA", self.screen_size, (0, 0, 0, 0)) self.current_menu = Image.new("RGBA", self.screen_size, (0, 0, 0, 0)) self.fg_color = (255, 255, 255, 211) - self.bg_color = (0, 0, 0, 78) + self.bg_color = (0, 0, 0, 127) # set up connection to mpd here - TODO: this belongs elsewhere and should be passed or pulled in self.player_client = musicpd.MPDClient() self.player_client.connect() + # track battery voltage over time so we can figure out if we're charging the battery or not + self.previous_voltage = 0.0 + # track currently playing song's art path to eliminate unnecessary reloads of the same image file + self.current_art_path = None def getBatteryState(self): bus = smbus.SMBus(1) @@ -42,14 +46,16 @@ class SoundslabDisplay: swapped = struct.unpack("H", read))[0] capacity = swapped / 256 + self.previous_voltage = voltage + return (voltage, capacity) - def updateOverlay(self, battery_level_percent): + def updateOverlay(self): # initialize overlay im_overlay = Image.new("RGBA", self.screen_size, (0, 0, 0, 0)) font = ImageFont.truetype(font='/usr/share/fonts/truetype/hack/Hack-Bold.ttf', size=18) - small_font = ImageFont.truetype(font='/usr/share/fonts/truetype/hack/Hack-Regular.ttf', size=12) + small_font = ImageFont.truetype(font='/usr/share/fonts/truetype/hack/Hack-Regular.ttf', size=14) overlay = ImageDraw.Draw(im_overlay) # draw four rects on edges of screen_size @@ -69,11 +75,12 @@ class SoundslabDisplay: overlay.text((10, 0), track_and_queue, font=font, fill=self.fg_color) # add battery level + previous_voltage = self.previous_voltage (voltage, capacity) = self.getBatteryState() battery_display = '{:02.2f}'.format(capacity) + '%' - (voltage2, capacity2) = self.getBatteryState() battery_display_offset = 80 - if voltage2 > voltage: + print("Battery: voltage: " + str(voltage) + " prev voltage: " + str(previous_voltage) + " capacity: " + str(capacity)) + if voltage > previous_voltage: # we're probably charging! battery_display = 'Chrg ' + battery_display battery_display_offset = 130 @@ -95,13 +102,21 @@ class SoundslabDisplay: def updateAlbumArt(self): current_status = self.player_client.status() song_data = self.player_client.playlistid(current_status['songid']) - print(song_data[0]['file']) path_info = song_data[0]['file'].split('/') - print(path_info) cover_image_file = '/media/usb0/' + path_info[0] + '/' + path_info[1] + '/cover.jpg' - im = Image.open(cover_image_file).convert("RGBA") - image = im.resize(self.screen_size) - self.current_background = image.copy() + if cover_image_file != self.current_art_path: + self.current_art_path = cover_image_file + im = Image.open(cover_image_file).convert("RGBA") + image = im.resize((200, 200)) + # get dominant color from album art + #test_image = im.convert("RGB") + #test_image.resize((1, 1), resample=0) + #dominant_color = test_image.getpixel((0, 0)) + # get top left pixel color instead and use that! + dominant_color = image.getpixel((0, 0)) + backing = Image.new("RGBA", self.screen_size, dominant_color) + backing.paste(image, (20, 20)) + self.current_background = backing.copy() def updateDisplay(self): tempoutput = Image.alpha_composite(self.current_background, self.current_overlay)