Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
-
import numpy as np
|
| 4 |
import os
|
| 5 |
import shutil
|
| 6 |
import random
|
| 7 |
|
| 8 |
-
def get_dominant_color(image):
|
| 9 |
-
"""Get the dominant color of an image as a single RGB value or return None."""
|
| 10 |
-
try:
|
| 11 |
-
# Convert the image to a smaller size to simplify color calculation
|
| 12 |
-
small_image = image.resize((1, 1)) # Reduce to 1x1 pixel
|
| 13 |
-
dominant_color = small_image.getpixel((0, 0)) # Get the only pixel's color
|
| 14 |
-
return dominant_color
|
| 15 |
-
except:
|
| 16 |
-
return None
|
| 17 |
-
|
| 18 |
def resize_and_pad(image, target_size):
|
| 19 |
"""Resize the image and pad it to match target size."""
|
| 20 |
img = image.convert("RGB")
|
|
@@ -33,13 +22,11 @@ def resize_and_pad(image, target_size):
|
|
| 33 |
# Use high-quality resampling for resizing
|
| 34 |
img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
if dominant_color is None:
|
| 39 |
-
dominant_color = (255, 255, 255) if random.choice([True, False]) else (0, 0, 0)
|
| 40 |
|
| 41 |
# Create a new image with the determined background color
|
| 42 |
-
new_img = Image.new("RGB", target_size,
|
| 43 |
offset = ((target_size[0] - new_width) // 2, (target_size[1] - new_height) // 2)
|
| 44 |
new_img.paste(img, offset)
|
| 45 |
return new_img
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image
|
|
|
|
| 3 |
import os
|
| 4 |
import shutil
|
| 5 |
import random
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def resize_and_pad(image, target_size):
|
| 8 |
"""Resize the image and pad it to match target size."""
|
| 9 |
img = image.convert("RGB")
|
|
|
|
| 22 |
# Use high-quality resampling for resizing
|
| 23 |
img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 24 |
|
| 25 |
+
# Randomly select black or white as the background color
|
| 26 |
+
padding_color = (255, 255, 255) if random.choice([True, False]) else (0, 0, 0)
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Create a new image with the determined background color
|
| 29 |
+
new_img = Image.new("RGB", target_size, padding_color)
|
| 30 |
offset = ((target_size[0] - new_width) // 2, (target_size[1] - new_height) // 2)
|
| 31 |
new_img.paste(img, offset)
|
| 32 |
return new_img
|