Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -162,10 +162,18 @@ def prepare_image_and_mask(image, width, height, overlap_percentage, resize_opti
|
|
| 162 |
margin_x = max(0, min(margin_x + x_offset, target_size[0] - new_width))
|
| 163 |
margin_y = max(0, min(margin_y + y_offset, target_size[1] - new_height))
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
background = Image.new('RGB', target_size, (255, 255, 255))
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
-
mask = Image.new('L', target_size, 255)
|
| 169 |
mask_draw = ImageDraw.Draw(mask)
|
| 170 |
white_gaps_patch = 2
|
| 171 |
|
|
@@ -228,8 +236,18 @@ def outpaint(image, width, height, overlap_percentage, num_inference_steps, resi
|
|
| 228 |
background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom, x_offset, y_offset)
|
| 229 |
if not can_expand(background.width, background.height, width, height, alignment):
|
| 230 |
alignment = "Middle"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
cnet_image = background.copy()
|
| 232 |
-
cnet_image.paste(0, (0, 0),
|
|
|
|
| 233 |
final_prompt = f"score_9, score_8_up, score_7_up, {prompt_input} , high quality, 4k"
|
| 234 |
print(f"Outpainting using SDXL model: {pipe.config.model_name}")
|
| 235 |
(
|
|
@@ -247,8 +265,11 @@ def outpaint(image, width, height, overlap_percentage, num_inference_steps, resi
|
|
| 247 |
num_inference_steps=num_inference_steps
|
| 248 |
):
|
| 249 |
yield cnet_image, image
|
|
|
|
|
|
|
| 250 |
image = image.convert("RGBA")
|
| 251 |
-
cnet_image.paste(image, (0, 0),
|
|
|
|
| 252 |
yield background, cnet_image
|
| 253 |
|
| 254 |
@spaces.GPU(duration=7)
|
|
|
|
| 162 |
margin_x = max(0, min(margin_x + x_offset, target_size[0] - new_width))
|
| 163 |
margin_y = max(0, min(margin_y + y_offset, target_size[1] - new_height))
|
| 164 |
|
| 165 |
+
if image.mode == "RGBA":
|
| 166 |
+
background = Image.new("RGBA", target_size, (255, 255, 255, 255)
|
| 167 |
+
else:
|
| 168 |
+
background = Image.new("RGB", target_size, (255, 255, 255)
|
| 169 |
+
|
| 170 |
background = Image.new('RGB', target_size, (255, 255, 255))
|
| 171 |
+
# Paste the source (resized image) onto the background
|
| 172 |
+
background.paste(source, (margin_x, margin_y)
|
| 173 |
+
|
| 174 |
+
# Create the generated mask (L mode)
|
| 175 |
+
mask = Image.new("L", target_size, 255)
|
| 176 |
|
|
|
|
| 177 |
mask_draw = ImageDraw.Draw(mask)
|
| 178 |
white_gaps_patch = 2
|
| 179 |
|
|
|
|
| 236 |
background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom, x_offset, y_offset)
|
| 237 |
if not can_expand(background.width, background.height, width, height, alignment):
|
| 238 |
alignment = "Middle"
|
| 239 |
+
|
| 240 |
+
# Extract original alpha from the input image
|
| 241 |
+
original_alpha = background.split()[3] if background.mode == "RGBA" else Image.new("L", background.size, 255)
|
| 242 |
+
original_alpha_mask = original_alpha.point(lambda p: 0 if p < 255 else 255) # 0 where transparent
|
| 243 |
+
|
| 244 |
+
# Combine original and generated mask
|
| 245 |
+
combined_mask = ImageChops.logical_or(original_alpha_mask, mask) # 0 where to fill
|
| 246 |
+
|
| 247 |
+
# Use the combined_mask in the pipeline
|
| 248 |
cnet_image = background.copy()
|
| 249 |
+
cnet_image.paste(0, (0, 0), combined_mask) # Overlay black on combined_mask area
|
| 250 |
+
|
| 251 |
final_prompt = f"score_9, score_8_up, score_7_up, {prompt_input} , high quality, 4k"
|
| 252 |
print(f"Outpainting using SDXL model: {pipe.config.model_name}")
|
| 253 |
(
|
|
|
|
| 265 |
num_inference_steps=num_inference_steps
|
| 266 |
):
|
| 267 |
yield cnet_image, image
|
| 268 |
+
# Invert the combined_mask and paste the generated image back
|
| 269 |
+
filled_mask = combined_mask.point(lambda p: 255 - p)
|
| 270 |
image = image.convert("RGBA")
|
| 271 |
+
cnet_image.paste(image, (0, 0), filled_mask)
|
| 272 |
+
|
| 273 |
yield background, cnet_image
|
| 274 |
|
| 275 |
@spaces.GPU(duration=7)
|