hansoneze commited on
Commit
2737f09
·
1 Parent(s): e85935e

Switch to Gemini + update requirements

Browse files
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=False):
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 the edges (soft transition)
102
  mask = mask.filter(ImageFilter.GaussianBlur(radius=2))
103
-
104
- # Rebuild foreground with feathered alpha
105
  fg.putalpha(mask)
106
 
107
  # ---------------------------
108
- # Merge main photo with background
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- #inputs=[input_img, bg_choices, bg_upload, logo_upload, logo_transparency, logo_position, brand_color],
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

  • SHA256: 359564112a2a01811692bba02ea1b852f0c57d6b0c1b776f5613527831c3c4e4
  • Pointer size: 132 Bytes
  • Size of remote file: 1.12 MB
results/result_20250910230231.png DELETED

Git LFS Details

  • SHA256: cefd68d9a047dceff646a87084d3a03b2448482bcc3074009dee19136190547b
  • Pointer size: 132 Bytes
  • Size of remote file: 1.31 MB
results/result_20250910230304.png DELETED

Git LFS Details

  • SHA256: aa5cac83b5efff4e2a0569e7a63ffab7357234e32fc53dec032057de66f5aad0
  • Pointer size: 132 Bytes
  • Size of remote file: 1.26 MB
results/result_20250910230329.png DELETED

Git LFS Details

  • SHA256: cefd68d9a047dceff646a87084d3a03b2448482bcc3074009dee19136190547b
  • Pointer size: 132 Bytes
  • Size of remote file: 1.31 MB
results/result_20250910230416.png DELETED

Git LFS Details

  • SHA256: 6f489ad673ff34f4f9225bbddb754393c1ed81d98691dd4fd017013a8f9a1c3f
  • Pointer size: 132 Bytes
  • Size of remote file: 1.18 MB
results/result_20250910230447.png DELETED

Git LFS Details

  • SHA256: 3a492db0489331e2d4755606b63d25c957e7d75e9dbad1f9c8194e8ccd4a781d
  • Pointer size: 131 Bytes
  • Size of remote file: 636 kB
results/result_20250911001224.png DELETED

Git LFS Details

  • SHA256: 57d3906cb60137cbf04d04655e5a27fa9daadb04c5132912ed2be820c7215d69
  • Pointer size: 133 Bytes
  • Size of remote file: 13 MB
results/result_20250911002426.png DELETED

Git LFS Details

  • SHA256: c4fda397107238d0292e95fd3aacf407515e21fd50048219c7abdba93e11018c
  • Pointer size: 133 Bytes
  • Size of remote file: 12.8 MB
results/result_20250911002933.png DELETED

Git LFS Details

  • SHA256: ffdedf54f53d4fd7443a9ef0704e520cae63127ce14d08bf710a6cb20544af8e
  • Pointer size: 132 Bytes
  • Size of remote file: 1.32 MB
results/result_20250911010759.png DELETED

Git LFS Details

  • SHA256: 3e771da0a26b14348b1039ae05480a67274bffad62b281e740767c27f4eea469
  • Pointer size: 132 Bytes
  • Size of remote file: 1.69 MB
results/result_20250911010857.png DELETED

Git LFS Details

  • SHA256: 3c88b3f6e72ff08862a9592184d033605de0fe01f58d0d72782db7acb4253605
  • Pointer size: 132 Bytes
  • Size of remote file: 1.7 MB
results/result_20250911015256.png DELETED

Git LFS Details

  • SHA256: abb119dae1971312ac1efa992e6338f77d996b1ebf7b40cae671f3c4cd5186af
  • Pointer size: 132 Bytes
  • Size of remote file: 9.61 MB
results/{result_20250910225642.png → result_20250912095058.png} RENAMED
File without changes
results/result_20250910230345.png → uploads/upload_1757667056.png RENAMED
File without changes