-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI.py
More file actions
175 lines (141 loc) · 6.75 KB
/
UI.py
File metadata and controls
175 lines (141 loc) · 6.75 KB
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
#!/usr/bin/env python3
"""
UI Builder Module.
Generates an image based on Spotify playback data, including album cover, track name,
artist, and playback time. Uses the Pillow library for image manipulation with a black
background and teal-colored text.
"""
from PIL import Image, ImageDraw, ImageFont
import requests
from io import BytesIO
import os
from config import (
FONT_FILENAME, FONT_SIZE_TITLE, FONT_SIZE_ARTIST, FONT_SIZE_TIME, SCROLL_SPEED,
ALBUM_COVER_POSITION, ALBUM_COVER_SIZE, TRACK_NAME_POSITION,
ARTIST_NAME_POSITION, PLAYBACK_TIME_POSITION, IMAGE_WIDTH, IMAGE_HEIGHT,
BACKGROUND_COLOR, TEXT_COLOR, PLACEHOLDER_COLOR, PLACEHOLDER_TEXT, DEFAULT_MESSAGE
)
class UIGenerator:
"""
UIGenerator creates UI images based on Spotify playback data.
"""
def __init__(self):
"""Initializes the UIGenerator with necessary fonts."""
font_path = os.path.join(os.path.dirname(__file__), 'Fonts', FONT_FILENAME)
try:
self.font_title = ImageFont.truetype(font_path, FONT_SIZE_TITLE)
self.font_artist = ImageFont.truetype(font_path, FONT_SIZE_ARTIST)
self.font_time = ImageFont.truetype(font_path, FONT_SIZE_TIME)
self.ARTIST_OFFSET = 0
self.TRACK_OFFSET = 0
except IOError:
raise FileNotFoundError(f"Font file not found at {font_path}. Ensure the .ttf file is in the correct directory.")
def fetch_album_cover(self, url):
"""
Fetches the album cover image from the provided URL and resizes it.
Args:
url (str): URL of the album cover image.
Returns:
Image or None: Resized PIL Image object or None if fetching fails.
"""
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
return Image.open(BytesIO(response.content)).resize(ALBUM_COVER_SIZE)
except (requests.RequestException, Exception) as e:
print(f"Failed to fetch album cover: {e}")
return None
@staticmethod
def format_time(seconds):
"""
Formats seconds into M:SS string.
Args:
seconds (int): Number of seconds.
Returns:
str: Formatted time string.
"""
seconds = round(seconds)
mins, secs = divmod(seconds, 60)
return f"{mins}:{secs:02}"
def marquee_text(self, draw, text, font, position, width, speed, offset):
"""
Marquee scroll text continuously in a loop.
Args:
draw (ImageDraw): The ImageDraw object.
text (str): The text to scroll.
font (ImageFont): The font of the text.
position (tuple): (x, y) position of the text.
width (int): The available width for the text.
speed (int): The speed of the scroll.
offset (int): Current scroll offset.
Returns:
int: Updated scroll offset.
"""
total_text_width = draw.textbbox((0, 0), text, font=font)[2]
if total_text_width > (width - (ALBUM_COVER_SIZE[0] + ALBUM_COVER_POSITION[0] + ALBUM_COVER_POSITION[1])):
offset = (offset + speed) % (total_text_width + width)
current_x = position[0] - offset
any_visible = False
for char in text:
char_width = draw.textbbox((0, 0), char, font=font)[2]
if current_x + char_width > 85: # Adjust this value as needed
draw.text((current_x, position[1]), char, font=font, fill=TEXT_COLOR)
any_visible = True
current_x += char_width
if not any_visible:
offset = 0
return offset
else:
draw.text(position, text, font=font, fill=TEXT_COLOR)
return 0
def reset_offsets(self):
"""
Resets the scrolling offsets for track and artist names.
"""
self.ARTIST_OFFSET = 0
self.TRACK_OFFSET = 0
def create_ui_image(self, playback_data):
"""
Creates the UI image based on playback data.
Args:
playback_data (dict): Data containing track information.
Returns:
Image: Generated UI image.
"""
img = Image.new('RGB', (IMAGE_WIDTH, IMAGE_HEIGHT), color=BACKGROUND_COLOR)
draw = ImageDraw.Draw(img)
album_cover_url = playback_data.get('album_cover_url')
album_cover = self.fetch_album_cover(album_cover_url) if album_cover_url else None
if album_cover:
img.paste(album_cover, ALBUM_COVER_POSITION)
else:
draw.rectangle(
[ALBUM_COVER_POSITION, (ALBUM_COVER_POSITION[0] + ALBUM_COVER_SIZE[0],
ALBUM_COVER_POSITION[1] + ALBUM_COVER_SIZE[1])],
fill=PLACEHOLDER_COLOR
)
bbox = draw.textbbox((0, 0), PLACEHOLDER_TEXT, font=self.font_time)
text_x = ALBUM_COVER_POSITION[0] + (ALBUM_COVER_SIZE[0] - (bbox[2] - bbox[0])) // 2
text_y = ALBUM_COVER_POSITION[1] + (ALBUM_COVER_SIZE[1] - (bbox[3] - bbox[1])) // 2
draw.text((text_x, text_y), PLACEHOLDER_TEXT, font=self.font_time, fill=TEXT_COLOR)
track_name = playback_data.get('track_name', 'Unknown Track')
artist_name = playback_data.get('artist', 'Unknown Artist')
self.TRACK_OFFSET = self.marquee_text(draw, track_name, self.font_title, TRACK_NAME_POSITION, IMAGE_WIDTH, SCROLL_SPEED, self.TRACK_OFFSET)
self.ARTIST_OFFSET = self.marquee_text(draw, artist_name, self.font_artist, ARTIST_NAME_POSITION, IMAGE_WIDTH, SCROLL_SPEED, self.ARTIST_OFFSET)
duration_sec = playback_data.get('duration_ms', 0) // 1000
progress_sec = playback_data.get('progress_ms', 0) // 1000
playback_time_text = f"{self.format_time(progress_sec)} / {self.format_time(duration_sec)}"
draw.text(PLAYBACK_TIME_POSITION, playback_time_text, font=self.font_time, fill=TEXT_COLOR)
return img
def create_default_image(self):
"""
Creates a default UI image when no music is playing.
Returns:
Image: PIL Image object representing the default UI.
"""
img = Image.new('RGB', (IMAGE_WIDTH, IMAGE_HEIGHT), color=BACKGROUND_COLOR)
draw = ImageDraw.Draw(img)
bbox = draw.textbbox((0, 0), DEFAULT_MESSAGE, font=self.font_title)
position = ((IMAGE_WIDTH - (bbox[2] - bbox[0])) // 2, (IMAGE_HEIGHT - (bbox[3] - bbox[1])) // 2)
draw.text(position, DEFAULT_MESSAGE, font=self.font_title, fill=TEXT_COLOR)
return img