Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -241,20 +241,23 @@ def outpaint(image, width, height, overlap_percentage, num_inference_steps, resi
|
|
241 |
if not can_expand(background.width, background.height, width, height, alignment):
|
242 |
alignment = "Middle"
|
243 |
|
244 |
-
#
|
245 |
original_alpha = background.split()[3] if background.mode == "RGBA" else Image.new("L", background.size, 255)
|
246 |
-
|
247 |
-
#
|
248 |
-
#
|
249 |
-
|
|
|
|
|
|
|
|
|
250 |
if cnet_image.mode == "RGBA":
|
251 |
-
#
|
252 |
-
|
253 |
-
cnet_image =
|
254 |
-
cnet_image.putalpha(alpha) # Replace the alpha channel
|
255 |
else:
|
256 |
-
# If no alpha, just use the background
|
257 |
-
cnet_image = background
|
258 |
# Use the combined_mask in the pipeline
|
259 |
#cnet_image = background.copy()
|
260 |
# cnet_image.paste(0, (0, 0), combined_mask) # Overlay black on combined_mask area
|
@@ -276,12 +279,11 @@ def outpaint(image, width, height, overlap_percentage, num_inference_steps, resi
|
|
276 |
num_inference_steps=num_inference_steps
|
277 |
):
|
278 |
yield cnet_image, image
|
279 |
-
# Invert the combined_mask and paste the generated image back
|
280 |
-
filled_mask = combined_mask.point(lambda p: 255 - p)
|
281 |
-
# image = image.convert("RGBA")
|
282 |
-
# cnet_image.paste(image, (0, 0), filled_mask)
|
283 |
image = image.convert("RGBA")
|
284 |
-
|
|
|
|
|
|
|
285 |
yield background, cnet_image
|
286 |
|
287 |
@spaces.GPU(duration=7)
|
|
|
241 |
if not can_expand(background.width, background.height, width, height, alignment):
|
242 |
alignment = "Middle"
|
243 |
|
244 |
+
# Original alpha is 0 where transparent (or 255 where opaque)
|
245 |
original_alpha = background.split()[3] if background.mode == "RGBA" else Image.new("L", background.size, 255)
|
246 |
+
|
247 |
+
# Generated mask is 0 where you want to paint (from image["layers"][0])
|
248 |
+
# If "layers"[0] is not alpha, convert it to L-mode
|
249 |
+
mask = mask.convert("L") if mask.mode != "L" else mask
|
250 |
+
|
251 |
+
# Combine the two masks: 0 where either the original is transparent OR the generated mask is active
|
252 |
+
combined_mask = ImageChops.logical_or(original_alpha, mask)
|
253 |
+
# After generating the combined_mask
|
254 |
if cnet_image.mode == "RGBA":
|
255 |
+
# Create a new alpha channel based on the mask
|
256 |
+
new_alpha = combined_mask.convert("L")
|
257 |
+
cnet_image = Image.merge("RGBA", cnet_image.split()[:3] + (new_alpha,))
|
|
|
258 |
else:
|
259 |
+
# If no alpha, you can't use the mask — just use the background as is
|
260 |
+
cnet_image = background.copy()
|
261 |
# Use the combined_mask in the pipeline
|
262 |
#cnet_image = background.copy()
|
263 |
# cnet_image.paste(0, (0, 0), combined_mask) # Overlay black on combined_mask area
|
|
|
279 |
num_inference_steps=num_inference_steps
|
280 |
):
|
281 |
yield cnet_image, image
|
|
|
|
|
|
|
|
|
282 |
image = image.convert("RGBA")
|
283 |
+
# If the pipeline uses alpha as the mask, invert it to get 255 where to paint
|
284 |
+
filled_mask = combined_mask.point(lambda p: 255 - p)
|
285 |
+
# Paste the result using the mask to preserve transparency
|
286 |
+
cnet_image.paste(image, (0, 0), filled_mask) # Only modify areas where filled_mask is 255 (i.e., original transparent + generated mask area)
|
287 |
yield background, cnet_image
|
288 |
|
289 |
@spaces.GPU(duration=7)
|