Spaces:
Build error
Build error
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -261,23 +261,63 @@ def overlay_ghost_mask(mask_img, background_img):
|
|
| 261 |
bg_width, bg_height = background_img.size
|
| 262 |
mask_width, mask_height = mask_img.size
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
bg_ratio = bg_width / bg_height
|
| 265 |
mask_ratio = mask_width / mask_height
|
| 266 |
|
| 267 |
-
if mask_ratio
|
| 268 |
-
|
| 269 |
-
|
|
|
|
| 270 |
else:
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
return result
|
| 283 |
|
|
|
|
| 261 |
bg_width, bg_height = background_img.size
|
| 262 |
mask_width, mask_height = mask_img.size
|
| 263 |
|
| 264 |
+
if bg_width < mask_width or bg_height < mask_height:
|
| 265 |
+
bg_ratio = bg_width / bg_height
|
| 266 |
+
mask_ratio = mask_width / mask_height
|
| 267 |
+
|
| 268 |
+
if mask_ratio > bg_ratio:
|
| 269 |
+
new_bg_height = int(bg_width / mask_ratio)
|
| 270 |
+
new_bg_width = bg_width
|
| 271 |
+
else:
|
| 272 |
+
new_bg_width = int(bg_height * mask_ratio)
|
| 273 |
+
new_bg_height = bg_height
|
| 274 |
+
|
| 275 |
+
new_background = Image.new('RGBA', (new_bg_width, new_bg_height), (255, 255, 255, 255))
|
| 276 |
+
paste_x = (new_bg_width - bg_width) // 2
|
| 277 |
+
paste_y = (new_bg_height - bg_height) // 2
|
| 278 |
+
new_background.paste(background_img, (paste_x, paste_y))
|
| 279 |
+
background_img = new_background
|
| 280 |
+
bg_width, bg_height = new_bg_width, new_bg_height
|
| 281 |
+
else:
|
| 282 |
+
mask_ratio = mask_width / mask_height
|
| 283 |
+
bg_ratio = bg_width / bg_height
|
| 284 |
+
|
| 285 |
+
if bg_ratio > mask_ratio:
|
| 286 |
+
new_mask_height = int(mask_width / bg_ratio)
|
| 287 |
+
new_mask_width = mask_width
|
| 288 |
+
else:
|
| 289 |
+
new_mask_width = int(mask_height * bg_ratio)
|
| 290 |
+
new_mask_height = mask_height
|
| 291 |
+
|
| 292 |
+
new_mask = Image.new('RGBA', (new_mask_width, new_mask_height), (0, 0, 0, 0))
|
| 293 |
+
paste_x = (new_mask_width - mask_width) // 2
|
| 294 |
+
paste_y = (new_mask_height - mask_height) // 2
|
| 295 |
+
new_mask.paste(mask_img, (paste_x, paste_y))
|
| 296 |
+
mask_img = new_mask
|
| 297 |
+
mask_width, mask_height = new_mask_width, new_mask_height
|
| 298 |
+
|
| 299 |
bg_ratio = bg_width / bg_height
|
| 300 |
mask_ratio = mask_width / mask_height
|
| 301 |
|
| 302 |
+
if abs(mask_ratio - bg_ratio) < 0.01:
|
| 303 |
+
mask_resized = mask_img.resize((bg_width, bg_height), Image.Resampling.LANCZOS)
|
| 304 |
+
result = background_img.copy()
|
| 305 |
+
result.paste(mask_resized, (0, 0), mask_resized)
|
| 306 |
else:
|
| 307 |
+
if mask_ratio > bg_ratio:
|
| 308 |
+
new_mask_width = bg_width
|
| 309 |
+
new_mask_height = int(bg_width / mask_ratio)
|
| 310 |
+
else:
|
| 311 |
+
new_mask_height = bg_height
|
| 312 |
+
new_mask_width = int(bg_height * mask_ratio)
|
| 313 |
+
|
| 314 |
+
mask_resized = mask_img.resize((new_mask_width, new_mask_height), Image.Resampling.LANCZOS)
|
| 315 |
+
|
| 316 |
+
x_offset = (bg_width - new_mask_width) // 2
|
| 317 |
+
y_offset = (bg_height - new_mask_height) // 2
|
| 318 |
+
|
| 319 |
+
result = background_img.copy()
|
| 320 |
+
result.paste(mask_resized, (x_offset, y_offset), mask_resized)
|
| 321 |
|
| 322 |
return result
|
| 323 |
|