Switch to Gemini + update requirements
Browse files- app.py +35 -7
- results/result_20250910225723.png +0 -3
- results/result_20250910230231.png +0 -3
- results/result_20250910230304.png +0 -3
- results/result_20250910230329.png +0 -3
- results/result_20250910230416.png +0 -3
- results/result_20250910230447.png +0 -3
- results/result_20250911001224.png +0 -3
- results/result_20250911002426.png +0 -3
- results/result_20250911002933.png +0 -3
- results/result_20250911010759.png +0 -3
- results/result_20250911010857.png +0 -3
- results/result_20250911015256.png +0 -3
- results/{result_20250910225642.png → result_20250912095058.png} +2 -2
- results/result_20250910230345.png → uploads/upload_1757667056.png +2 -2
app.py
CHANGED
|
@@ -70,7 +70,7 @@ def replace_background(input_path, bg_choice):
|
|
| 70 |
return result_path
|
| 71 |
|
| 72 |
#def process_image(input_img, bg_choice, bg_upload, logo_upload, logo_transparency, logo_position, brand_color):
|
| 73 |
-
def process_image(input_img, bg_choice, bg_upload, logo_upload, logo_transparency, logo_position, blur_background
|
| 74 |
if input_img is None:
|
| 75 |
return []
|
| 76 |
|
|
@@ -98,14 +98,38 @@ def process_image(input_img, bg_choice, bg_upload, logo_upload, logo_transparenc
|
|
| 98 |
# Extract mask from alpha channel
|
| 99 |
mask = fg.split()[3]
|
| 100 |
|
| 101 |
-
# Feather
|
| 102 |
mask = mask.filter(ImageFilter.GaussianBlur(radius=2))
|
| 103 |
-
|
| 104 |
-
# Rebuild foreground with feathered alpha
|
| 105 |
fg.putalpha(mask)
|
| 106 |
|
| 107 |
# ---------------------------
|
| 108 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
# ---------------------------
|
| 110 |
result = Image.alpha_composite(bg, fg)
|
| 111 |
|
|
@@ -136,6 +160,7 @@ def process_image(input_img, bg_choice, bg_upload, logo_upload, logo_transparenc
|
|
| 136 |
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 137 |
result_path = os.path.join(RESULTS_DIR, f"result_{timestamp}.png")
|
| 138 |
result.save(result_path)
|
|
|
|
| 139 |
cleanup_old_files(RESULTS_DIR)
|
| 140 |
return [result_path]
|
| 141 |
|
|
@@ -223,6 +248,10 @@ with gr.Blocks(css="footer {display:none !important}") as demo:
|
|
| 223 |
value="Bottom-Right",
|
| 224 |
label="Logo Position"
|
| 225 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
#brand_color = gr.ColorPicker(label="Brand Colour")
|
| 227 |
|
| 228 |
btn = gr.Button("✨ Generate New Photo")
|
|
@@ -230,8 +259,7 @@ with gr.Blocks(css="footer {display:none !important}") as demo:
|
|
| 230 |
|
| 231 |
btn.click(
|
| 232 |
fn=process_image,
|
| 233 |
-
|
| 234 |
-
inputs=[input_img, bg_choices, bg_upload, logo_upload, logo_transparency, logo_position],
|
| 235 |
outputs=output_imgs
|
| 236 |
)
|
| 237 |
|
|
|
|
| 70 |
return result_path
|
| 71 |
|
| 72 |
#def process_image(input_img, bg_choice, bg_upload, logo_upload, logo_transparency, logo_position, brand_color):
|
| 73 |
+
def process_image(input_img, bg_choice, bg_upload, logo_upload, logo_transparency, logo_position, blur_background, blend_strength):
|
| 74 |
if input_img is None:
|
| 75 |
return []
|
| 76 |
|
|
|
|
| 98 |
# Extract mask from alpha channel
|
| 99 |
mask = fg.split()[3]
|
| 100 |
|
| 101 |
+
# Feather edges for smooth transition
|
| 102 |
mask = mask.filter(ImageFilter.GaussianBlur(radius=2))
|
|
|
|
|
|
|
| 103 |
fg.putalpha(mask)
|
| 104 |
|
| 105 |
# ---------------------------
|
| 106 |
+
# Color matching (adjust subject brightness to background)
|
| 107 |
+
# ---------------------------
|
| 108 |
+
try:
|
| 109 |
+
from PIL import ImageStat, ImageEnhance
|
| 110 |
+
|
| 111 |
+
# Average brightness of background
|
| 112 |
+
stat_bg = ImageStat.Stat(bg.convert("L"))
|
| 113 |
+
bg_brightness = stat_bg.mean[0]
|
| 114 |
+
|
| 115 |
+
# Average brightness of foreground
|
| 116 |
+
stat_fg = ImageStat.Stat(fg.convert("L"))
|
| 117 |
+
fg_brightness = stat_fg.mean[0]
|
| 118 |
+
|
| 119 |
+
if fg_brightness > 0:
|
| 120 |
+
brightness_ratio = bg_brightness / fg_brightness
|
| 121 |
+
|
| 122 |
+
# Apply brightness adjustment with user-controlled strength
|
| 123 |
+
enhancer = ImageEnhance.Brightness(fg)
|
| 124 |
+
adjusted = enhancer.enhance(brightness_ratio)
|
| 125 |
+
|
| 126 |
+
# Blend original fg with adjusted fg
|
| 127 |
+
fg = Image.blend(fg, adjusted, alpha=blend_strength)
|
| 128 |
+
except Exception as e:
|
| 129 |
+
print("Color match failed:", e)
|
| 130 |
+
|
| 131 |
+
# ---------------------------
|
| 132 |
+
# Merge subject with background
|
| 133 |
# ---------------------------
|
| 134 |
result = Image.alpha_composite(bg, fg)
|
| 135 |
|
|
|
|
| 160 |
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 161 |
result_path = os.path.join(RESULTS_DIR, f"result_{timestamp}.png")
|
| 162 |
result.save(result_path)
|
| 163 |
+
|
| 164 |
cleanup_old_files(RESULTS_DIR)
|
| 165 |
return [result_path]
|
| 166 |
|
|
|
|
| 248 |
value="Bottom-Right",
|
| 249 |
label="Logo Position"
|
| 250 |
)
|
| 251 |
+
|
| 252 |
+
blend_strength = gr.Slider(0, 1, value=0.5, step=0.1, label="Blending Strength (Realism)")
|
| 253 |
+
blur_background = gr.Checkbox(label="Blur Background", value=False)
|
| 254 |
+
|
| 255 |
#brand_color = gr.ColorPicker(label="Brand Colour")
|
| 256 |
|
| 257 |
btn = gr.Button("✨ Generate New Photo")
|
|
|
|
| 259 |
|
| 260 |
btn.click(
|
| 261 |
fn=process_image,
|
| 262 |
+
inputs=[input_img, bg_choices, bg_upload, logo_upload, logo_transparency, logo_position, blur_background, blend_strength],
|
|
|
|
| 263 |
outputs=output_imgs
|
| 264 |
)
|
| 265 |
|
results/result_20250910225723.png
DELETED
Git LFS Details
|
results/result_20250910230231.png
DELETED
Git LFS Details
|
results/result_20250910230304.png
DELETED
Git LFS Details
|
results/result_20250910230329.png
DELETED
Git LFS Details
|
results/result_20250910230416.png
DELETED
Git LFS Details
|
results/result_20250910230447.png
DELETED
Git LFS Details
|
results/result_20250911001224.png
DELETED
Git LFS Details
|
results/result_20250911002426.png
DELETED
Git LFS Details
|
results/result_20250911002933.png
DELETED
Git LFS Details
|
results/result_20250911010759.png
DELETED
Git LFS Details
|
results/result_20250911010857.png
DELETED
Git LFS Details
|
results/result_20250911015256.png
DELETED
Git LFS Details
|
results/{result_20250910225642.png → result_20250912095058.png}
RENAMED
|
File without changes
|
results/result_20250910230345.png → uploads/upload_1757667056.png
RENAMED
|
File without changes
|