import sys import spotipy import spotipy.util as util import config as cfg from PIL import Image, ImageDraw, ImageFont import asyncio from rgbmatrix import RGBMatrix, RGBMatrixOptions import threading import requests from io import BytesIO import time client_id = cfg.client_id client_secret = cfg.client_secret redirect_uri = cfg.redirect_uri username = cfg.username scope = 'user-read-currently-playing' options = RGBMatrixOptions() options.rows = 32 options.cols = 64 options.chain_length = 6 options.parallel = 2 options.hardware_mapping = 'regular' options.pixel_mapper_config = 'U-mapper' options.limit_refresh_rate_hz = 240 options.gpio_slowdown = 4 options.brightness = 50 screen = RGBMatrix(options = options) song_name="" artist_name="" is_playing = False token = util.prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri) font = ImageFont.load_default() def get_track(): print('getting current song info') sp = spotipy.Spotify(auth=token) track = sp.currently_playing() global is_playing is_playing = track['is_playing'] global song_name song_name = track['item']['name'] global artist_name artist_name = track['item']['artists'][0]['name'] for i in range(len(track['item']['artists'])-1): artist_name +=', ' + track['item']['artists'][i+1]['name'] #print(track['item']['name'] + ' - ' + track['item']['artists'][0]['name']) #print('getting image') image_url = track['item']['album']['images'][0]['url'] response = requests.get(image_url) image = Image.open(BytesIO(response.content)) #print(image_url) image = image.resize((32,32)) return image def showImage(image, offset_x = 0, offset_y = 0): image = image.convert('RGB') width, height = image.size for x in range(width): for y in range(height): r, g, b = image.getpixel((x, y)) screen.SetPixel(x+offset_x,y+offset_y,r,g,b) #if token: # sp = spotipy.Spotify(auth=token) # track = sp.currently_playing() # print(track['is_playing']) # print(track['item']['name'] + ' - ' + track['item']['artists'][0]['name']) # imageString = requests.get(track['item']['album']['images'][0]['url']) # image = Image.open(BytesIO(imageString.content)) # image = image.resize((32,32)) # #image.save("thumbnail.jpg","JPEG") #else: # print("Can't get token for", username) #def main(): # x = threading.Thread(target=draw) # x.start() def drawCanvas(): canvas = Image.new('RGB', (192, 128), color = 'black') cover_art = get_track() if(is_playing): canvas.paste(cover_art,(0,96)) songinfo = ImageDraw.Draw(canvas) songinfo_offset_x = 33 songinfo_offset_y = 101 songinfo.text((songinfo_offset_x,songinfo_offset_y+10), song_name) songinfo.text((songinfo_offset_x,songinfo_offset_y), artist_name) #scrolling_text(song_name,songinfo_offset_x, songinfo_offset_y+10) if(font.getsize(song_name)[0]>160): x = threading.Thread(target=scrolling_text,args=(song_name,songinfo_offset_x,songinfo_offset_y+10), daemon=True) x.start() print('scrolling text started') if(font.getsize(artist_name)[0]>160): y = threading.Thread(target=scrolling_text,args=(artist_name,songinfo_offset_x,songinfo_offset_y)) y.start() showImage(canvas) def scrolling_text(text,offset_x=0,offset_y=0): for i in range(font.getsize(text)[0]-160): scrolled_text_canvas = Image.new('RGB', (160, 32), color = 'black') scrolled_text = ImageDraw.Draw(scrolled_text_canvas) scrolled_text.text((-i,0), text) showImage(scrolled_text_canvas, offset_x, offset_y) time.sleep(0.1) #print('scrolled '+str(i)+' px') time.sleep(2) for i in range(font.getsize(text)[0]-160): scrolled_text_canvas = Image.new('RGB', (160, 32), color = 'black') scrolled_text = ImageDraw.Draw(scrolled_text_canvas) scrolled_text.text(((-(font.getsize(text)[0]-160))+i+1,0), text) showImage(scrolled_text_canvas, offset_x, offset_y) time.sleep(0.1) #print('scrolled '+str(i)+' px') if __name__ == "__main__": #main() print('led-matrix-viewer started') while True: drawCanvas() time.sleep(10)