Update app.py
Browse files
app.py
CHANGED
|
@@ -88,10 +88,11 @@ class ImageResizer:
|
|
| 88 |
new_width, new_height = resized_image.size
|
| 89 |
|
| 90 |
# Determine background color based on user selection
|
|
|
|
| 91 |
if png_bg_option == "black":
|
| 92 |
-
bg_color = (0, 0, 0)
|
| 93 |
elif png_bg_option == "white":
|
| 94 |
-
bg_color = (255, 255, 255)
|
| 95 |
elif png_bg_option == "custom" and custom_color:
|
| 96 |
bg_color = custom_color
|
| 97 |
elif png_bg_option == "auto" and (is_png or (hasattr(image, 'format') and image.format == 'PNG')):
|
|
@@ -99,12 +100,13 @@ class ImageResizer:
|
|
| 99 |
else:
|
| 100 |
bg_color = (255, 255, 255) # Default white
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# Create canvas with the determined background color
|
| 103 |
canvas = Image.new('RGB', (width, height), bg_color)
|
| 104 |
|
| 105 |
-
# Debug info
|
| 106 |
-
print(f"Debug: Background color applied: {bg_color}, PNG option: {png_bg_option}")
|
| 107 |
-
|
| 108 |
# Calculate position to center the image
|
| 109 |
x = (width - new_width) // 2
|
| 110 |
y = (height - new_height) // 2
|
|
@@ -160,16 +162,24 @@ def process_single_image(image, width, height, maintain_aspect, png_bg_option, c
|
|
| 160 |
if png_bg_option in ["black", "white", "custom"]:
|
| 161 |
is_png = True # Force PNG treatment for specific background choices
|
| 162 |
|
| 163 |
-
# Debug info (will be removed in production)
|
| 164 |
-
print(f"Debug: PNG detection - format: {getattr(pil_image, 'format', 'None')}, mode: {pil_image.mode}, is_png: {is_png}, bg_option: {png_bg_option}")
|
| 165 |
-
|
| 166 |
# Resize image
|
| 167 |
resized_image = resizer.resize_image(
|
| 168 |
pil_image, width, height, maintain_aspect,
|
| 169 |
png_bg_option, custom_color, is_png
|
| 170 |
)
|
| 171 |
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
except Exception as e:
|
| 175 |
return None, f"❌ Error processing image: {str(e)}"
|
|
|
|
| 88 |
new_width, new_height = resized_image.size
|
| 89 |
|
| 90 |
# Determine background color based on user selection
|
| 91 |
+
# Force explicit color selection to ensure it works
|
| 92 |
if png_bg_option == "black":
|
| 93 |
+
bg_color = (0, 0, 0) # Pure black
|
| 94 |
elif png_bg_option == "white":
|
| 95 |
+
bg_color = (255, 255, 255) # Pure white
|
| 96 |
elif png_bg_option == "custom" and custom_color:
|
| 97 |
bg_color = custom_color
|
| 98 |
elif png_bg_option == "auto" and (is_png or (hasattr(image, 'format') and image.format == 'PNG')):
|
|
|
|
| 100 |
else:
|
| 101 |
bg_color = (255, 255, 255) # Default white
|
| 102 |
|
| 103 |
+
# Ensure we have a valid color tuple
|
| 104 |
+
if not isinstance(bg_color, tuple) or len(bg_color) != 3:
|
| 105 |
+
bg_color = (255, 255, 255) # Fallback to white
|
| 106 |
+
|
| 107 |
# Create canvas with the determined background color
|
| 108 |
canvas = Image.new('RGB', (width, height), bg_color)
|
| 109 |
|
|
|
|
|
|
|
|
|
|
| 110 |
# Calculate position to center the image
|
| 111 |
x = (width - new_width) // 2
|
| 112 |
y = (height - new_height) // 2
|
|
|
|
| 162 |
if png_bg_option in ["black", "white", "custom"]:
|
| 163 |
is_png = True # Force PNG treatment for specific background choices
|
| 164 |
|
|
|
|
|
|
|
|
|
|
| 165 |
# Resize image
|
| 166 |
resized_image = resizer.resize_image(
|
| 167 |
pil_image, width, height, maintain_aspect,
|
| 168 |
png_bg_option, custom_color, is_png
|
| 169 |
)
|
| 170 |
|
| 171 |
+
# Get background color info for status message
|
| 172 |
+
bg_info = ""
|
| 173 |
+
if png_bg_option == "black":
|
| 174 |
+
bg_info = " with black background"
|
| 175 |
+
elif png_bg_option == "white":
|
| 176 |
+
bg_info = " with white background"
|
| 177 |
+
elif png_bg_option == "custom":
|
| 178 |
+
bg_info = f" with custom background {custom_color_hex}"
|
| 179 |
+
elif png_bg_option == "auto":
|
| 180 |
+
bg_info = " with auto-detected background"
|
| 181 |
+
|
| 182 |
+
return resized_image, f"✅ Image resized successfully to {width}x{height}{bg_info}!"
|
| 183 |
|
| 184 |
except Exception as e:
|
| 185 |
return None, f"❌ Error processing image: {str(e)}"
|