Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -102,7 +102,7 @@ def fill_image(image, model_selection):
|
|
| 102 |
cnet_image.paste(image, (0, 0), mask)
|
| 103 |
|
| 104 |
yield background, cnet_image
|
| 105 |
-
|
| 106 |
|
| 107 |
@spaces.GPU
|
| 108 |
def fill_image(image, model_selection):
|
|
@@ -162,6 +162,67 @@ def fill_image(image, model_selection):
|
|
| 162 |
cnet_image.paste(image, (0, 0), mask)
|
| 163 |
|
| 164 |
yield background, cnet_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
def clear_result():
|
| 167 |
return gr.update(value=None)
|
|
|
|
| 102 |
cnet_image.paste(image, (0, 0), mask)
|
| 103 |
|
| 104 |
yield background, cnet_image
|
| 105 |
+
|
| 106 |
|
| 107 |
@spaces.GPU
|
| 108 |
def fill_image(image, model_selection):
|
|
|
|
| 162 |
cnet_image.paste(image, (0, 0), mask)
|
| 163 |
|
| 164 |
yield background, cnet_image
|
| 165 |
+
"""
|
| 166 |
+
|
| 167 |
+
def fill_image(image, model_selection):
|
| 168 |
+
source = image
|
| 169 |
+
target_ratio = (16, 9) # Set the new target ratio to 16:9
|
| 170 |
+
target_width = 1280 # Adjust target width based on desired resolution
|
| 171 |
+
overlap = 48
|
| 172 |
+
fade_width = 24
|
| 173 |
+
max_height = 720 # Adjust max height instead of width
|
| 174 |
+
|
| 175 |
+
# Resize the image if it's taller than max_height
|
| 176 |
+
if source.height > max_height:
|
| 177 |
+
scale_factor = max_height / source.height
|
| 178 |
+
new_height = max_height
|
| 179 |
+
new_width = int(source.width * scale_factor)
|
| 180 |
+
source = source.resize((new_width, new_height), Image.LANCZOS)
|
| 181 |
+
|
| 182 |
+
# Calculate the required width for the 16:9 ratio
|
| 183 |
+
target_width = (source.height * target_ratio[0]) // target_ratio[1]
|
| 184 |
+
|
| 185 |
+
# Calculate margins (now left and right)
|
| 186 |
+
margin_x = (target_width - source.width) // 2
|
| 187 |
+
|
| 188 |
+
# Calculate new output size
|
| 189 |
+
output_size = (target_width, source.height)
|
| 190 |
+
|
| 191 |
+
# Create a white background
|
| 192 |
+
background = Image.new('RGB', output_size, (255, 255, 255))
|
| 193 |
+
|
| 194 |
+
# Calculate position to paste the original image
|
| 195 |
+
position = (margin_x, 0)
|
| 196 |
+
|
| 197 |
+
# Paste the original image onto the white background
|
| 198 |
+
background.paste(source, position)
|
| 199 |
+
|
| 200 |
+
# Create the mask
|
| 201 |
+
mask = Image.new('L', output_size, 255) # Start with all white
|
| 202 |
+
mask_draw = ImageDraw.Draw(mask)
|
| 203 |
+
mask_draw.rectangle([
|
| 204 |
+
(margin_x + overlap, overlap),
|
| 205 |
+
(margin_x + source.width - overlap, source.height - overlap)
|
| 206 |
+
], fill=0)
|
| 207 |
+
|
| 208 |
+
# Prepare the image for ControlNet
|
| 209 |
+
cnet_image = background.copy()
|
| 210 |
+
cnet_image.paste(0, (0, 0), mask)
|
| 211 |
+
|
| 212 |
+
for image in pipe(
|
| 213 |
+
prompt_embeds=prompt_embeds,
|
| 214 |
+
negative_prompt_embeds=negative_prompt_embeds,
|
| 215 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
| 216 |
+
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
|
| 217 |
+
image=cnet_image,
|
| 218 |
+
):
|
| 219 |
+
yield image, cnet_image
|
| 220 |
+
|
| 221 |
+
image = image.convert("RGBA")
|
| 222 |
+
cnet_image.paste(image, (0, 0), mask)
|
| 223 |
+
|
| 224 |
+
yield background, cnet_image
|
| 225 |
+
|
| 226 |
|
| 227 |
def clear_result():
|
| 228 |
return gr.update(value=None)
|