Improve NHL logo display and add detailed logging - Use RGBA mode with transparent overlays for logo compositing - Add comprehensive logging for logo loading and display process - Fix image compositing to properly display both logos

This commit is contained in:
ChuckBuilds
2025-04-18 11:41:21 -05:00
parent 6f4a735bee
commit 8323472e63

View File

@@ -80,10 +80,14 @@ class BaseNHLManager:
def _load_and_resize_logo(self, team_abbrev: str) -> Optional[Image.Image]: def _load_and_resize_logo(self, team_abbrev: str) -> Optional[Image.Image]:
"""Load and resize a team logo, with caching.""" """Load and resize a team logo, with caching."""
self.logger.info(f"Loading logo for team: {team_abbrev}")
if team_abbrev in self._logo_cache: if team_abbrev in self._logo_cache:
self.logger.info(f"Using cached logo for {team_abbrev}")
return self._logo_cache[team_abbrev] return self._logo_cache[team_abbrev]
logo_path = os.path.join(self.logo_dir, f"{team_abbrev}.png") logo_path = os.path.join(self.logo_dir, f"{team_abbrev}.png")
self.logger.info(f"Loading logo from path: {logo_path}")
try: try:
# Create test logos if they don't exist # Create test logos if they don't exist
@@ -102,11 +106,14 @@ class BaseNHLManager:
# Add team abbreviation # Add team abbreviation
draw.text((8, 8), team_abbrev, fill=(255, 255, 255, 255)) draw.text((8, 8), team_abbrev, fill=(255, 255, 255, 255))
logo.save(logo_path) logo.save(logo_path)
self.logger.info(f"Created and saved test logo for {team_abbrev}")
logo = Image.open(logo_path) logo = Image.open(logo_path)
self.logger.info(f"Opened logo for {team_abbrev}, initial size: {logo.size}, mode: {logo.mode}")
# Convert to RGBA if not already # Convert to RGBA if not already
if logo.mode != 'RGBA': if logo.mode != 'RGBA':
self.logger.info(f"Converting {team_abbrev} logo from {logo.mode} to RGBA")
logo = logo.convert('RGBA') logo = logo.convert('RGBA')
# Calculate max size based on display dimensions # Calculate max size based on display dimensions
@@ -115,6 +122,7 @@ class BaseNHLManager:
# Resize maintaining aspect ratio # Resize maintaining aspect ratio
logo.thumbnail((max_width, max_height), Image.Resampling.LANCZOS) logo.thumbnail((max_width, max_height), Image.Resampling.LANCZOS)
self.logger.info(f"Resized {team_abbrev} logo to: {logo.size}")
# Cache the resized logo # Cache the resized logo
self._logo_cache[team_abbrev] = logo self._logo_cache[team_abbrev] = logo
@@ -224,23 +232,31 @@ class BaseNHLManager:
def _draw_scorebug_layout(self): def _draw_scorebug_layout(self):
"""Draw the scorebug layout for the current game.""" """Draw the scorebug layout for the current game."""
try: try:
self.logger.info(f"Drawing scorebug for {self.current_game['away_abbr']} vs {self.current_game['home_abbr']}")
# Create a new black image for the main display # Create a new black image for the main display
main_img = Image.new('RGB', (self.display_width, self.display_height), 'black') main_img = Image.new('RGBA', (self.display_width, self.display_height), (0, 0, 0, 255))
draw = ImageDraw.Draw(main_img)
# Load logos once # Load logos once
home_logo = self._load_and_resize_logo(self.current_game["home_abbr"]) home_logo = self._load_and_resize_logo(self.current_game["home_abbr"])
away_logo = self._load_and_resize_logo(self.current_game["away_abbr"]) away_logo = self._load_and_resize_logo(self.current_game["away_abbr"])
# Create separate images for each logo self.logger.info(f"Home logo loaded: {home_logo is not None}, Away logo loaded: {away_logo is not None}")
home_img = Image.new('RGB', (self.display_width, self.display_height), 'black') if home_logo:
away_img = Image.new('RGB', (self.display_width, self.display_height), 'black') self.logger.info(f"Home logo size: {home_logo.size}, mode: {home_logo.mode}")
if away_logo:
self.logger.info(f"Away logo size: {away_logo.size}, mode: {away_logo.mode}")
# Draw home team logo (right side) # Draw home team logo (right side)
if home_logo: if home_logo:
home_x = 3 * self.display_width // 4 - home_logo.width // 2 home_x = 3 * self.display_width // 4 - home_logo.width // 2
home_y = self.display_height // 4 - home_logo.height // 2 home_y = self.display_height // 4 - home_logo.height // 2
home_img.paste(home_logo, (home_x, home_y), home_logo) self.logger.info(f"Pasting home logo at ({home_x}, {home_y})")
# Create a temporary image for the home logo
home_overlay = Image.new('RGBA', (self.display_width, self.display_height), (0, 0, 0, 0))
home_overlay.paste(home_logo, (home_x, home_y), home_logo)
main_img = Image.alpha_composite(main_img, home_overlay)
else: else:
self.logger.warning(f"Home logo is None for team {self.current_game['home_abbr']}") self.logger.warning(f"Home logo is None for team {self.current_game['home_abbr']}")
@@ -248,14 +264,18 @@ class BaseNHLManager:
if away_logo: if away_logo:
away_x = self.display_width // 4 - away_logo.width // 2 away_x = self.display_width // 4 - away_logo.width // 2
away_y = self.display_height // 4 - away_logo.height // 2 away_y = self.display_height // 4 - away_logo.height // 2
away_img.paste(away_logo, (away_x, away_y), away_logo) self.logger.info(f"Pasting away logo at ({away_x}, {away_y})")
# Create a temporary image for the away logo
away_overlay = Image.new('RGBA', (self.display_width, self.display_height), (0, 0, 0, 0))
away_overlay.paste(away_logo, (away_x, away_y), away_logo)
main_img = Image.alpha_composite(main_img, away_overlay)
else: else:
self.logger.warning(f"Away logo is None for team {self.current_game['away_abbr']}") self.logger.warning(f"Away logo is None for team {self.current_game['away_abbr']}")
# Combine the images # Convert to RGB for final display
main_img = Image.alpha_composite(main_img.convert('RGBA'), home_img.convert('RGBA'))
main_img = Image.alpha_composite(main_img, away_img.convert('RGBA'))
main_img = main_img.convert('RGB') main_img = main_img.convert('RGB')
draw = ImageDraw.Draw(main_img)
# Draw scores in the format "AWAY - HOME" # Draw scores in the format "AWAY - HOME"
home_score = str(self.current_game["home_score"]) home_score = str(self.current_game["home_score"])
@@ -270,7 +290,6 @@ class BaseNHLManager:
score_y = 3 * self.display_height // 4 - 10 score_y = 3 * self.display_height // 4 - 10
# Draw the score text # Draw the score text
draw = ImageDraw.Draw(main_img)
draw.text((score_x, score_y), score_text, font=self.fonts['score'], fill=(255, 255, 255)) draw.text((score_x, score_y), score_text, font=self.fonts['score'], fill=(255, 255, 255))
# Draw game status (period and time or "FINAL") # Draw game status (period and time or "FINAL")
@@ -284,6 +303,7 @@ class BaseNHLManager:
# Display the image # Display the image
self.display_manager.image.paste(main_img, (0, 0)) self.display_manager.image.paste(main_img, (0, 0))
self.display_manager.update_display() self.display_manager.update_display()
self.logger.info("Scorebug display completed")
except Exception as e: except Exception as e:
self.logger.error(f"Error displaying game: {e}", exc_info=True) self.logger.error(f"Error displaying game: {e}", exc_info=True)