From 06025296bb8b7d209e1dda11b5f1af0f557e5da0 Mon Sep 17 00:00:00 2001 From: Valerie Date: Sat, 24 May 2025 07:25:02 -0400 Subject: [PATCH] Enhance glass effect implementation in imgtools.py and default.py. Ensure images are in RGBA mode before applying effects, and update the glass effect application in the stats layer to use the glass background as a mask for improved visual consistency. --- levelup/generator/imgtools.py | 12 ++++++++---- levelup/generator/styles/default.py | 6 ++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/levelup/generator/imgtools.py b/levelup/generator/imgtools.py index fe8686f..d450819 100644 --- a/levelup/generator/imgtools.py +++ b/levelup/generator/imgtools.py @@ -390,17 +390,21 @@ def get_avg_duration(image: Image.Image) -> int: def create_glass_effect(image: Image.Image, opacity: float = 0.3, blur_radius: int = 10) -> Image.Image: """Create a modern glass effect with blur and transparency.""" - # Create a copy of the image to blur + # Create a copy of the image and ensure it's in RGBA mode glass = image.copy() + if glass.mode != "RGBA": + glass = glass.convert("RGBA") - # Apply gaussian blur - glass = glass.filter(ImageFilter.GaussianBlur(blur_radius)) + # Apply gaussian blur (must be in RGB mode for blur) + glass_rgb = glass.convert("RGB") + glass_rgb = glass_rgb.filter(ImageFilter.GaussianBlur(blur_radius)) + glass = glass_rgb.convert("RGBA") # Create a semi-transparent white overlay overlay = Image.new('RGBA', image.size, (255, 255, 255, int(255 * opacity))) # Composite the blurred image with the overlay - glass = Image.alpha_composite(glass.convert('RGBA'), overlay) + glass = Image.alpha_composite(glass, overlay) return glass diff --git a/levelup/generator/styles/default.py b/levelup/generator/styles/default.py index 627bc37..3dd2ec0 100644 --- a/levelup/generator/styles/default.py +++ b/levelup/generator/styles/default.py @@ -250,13 +250,15 @@ def generate_default_profile( # Create glass effect for stats area stats_bg = card.crop(stats_area) + if stats_bg.mode != "RGBA": + stats_bg = stats_bg.convert("RGBA") glass_bg = imgtools.create_glass_effect(stats_bg, opacity=0.3, blur_radius=20) - stats_layer.paste(glass_bg, stats_area) + stats_layer.paste(glass_bg, stats_area, glass_bg) # Use glass_bg as mask # Add rounded corners to the glass effect stats_mask = imgtools.get_rounded_corner_mask(glass_bg, radius=30) glass_bg.putalpha(stats_mask) - stats_layer.paste(glass_bg, stats_area, glass_bg) + stats_layer.paste(glass_bg, stats_area, glass_bg) # Use glass_bg as mask again # Draw stats with improved styling draw = ImageDraw.Draw(stats_layer)