Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -162,7 +162,7 @@ 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
|
@@ -222,8 +222,127 @@ def fill_image(image, model_selection):
|
|
222 |
cnet_image.paste(image, (0, 0), mask)
|
223 |
|
224 |
yield background, cnet_image
|
|
|
225 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
|
228 |
|
229 |
def clear_result():
|
@@ -237,8 +356,8 @@ css = """
|
|
237 |
"""
|
238 |
|
239 |
|
240 |
-
title = """<h1 align="center">Diffusers Image
|
241 |
-
<div align="center">
|
242 |
"""
|
243 |
|
244 |
with gr.Blocks(css=css) as demo:
|
@@ -257,12 +376,18 @@ with gr.Blocks(css=css) as demo:
|
|
257 |
interactive=False,
|
258 |
label="Generated Image",
|
259 |
)
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
266 |
|
267 |
run_button.click(
|
268 |
fn=clear_result,
|
@@ -270,7 +395,7 @@ with gr.Blocks(css=css) as demo:
|
|
270 |
outputs=result,
|
271 |
).then(
|
272 |
fn=fill_image,
|
273 |
-
inputs=[input_image, model_selection],
|
274 |
outputs=result,
|
275 |
)
|
276 |
|
|
|
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
|
|
|
222 |
cnet_image.paste(image, (0, 0), mask)
|
223 |
|
224 |
yield background, cnet_image
|
225 |
+
"""
|
226 |
|
227 |
+
def infer(image, model_selection, ratio_choice):
|
228 |
+
|
229 |
+
source = image
|
230 |
+
|
231 |
+
if ratio_choice == "16:9":
|
232 |
+
target_ratio = (16, 9) # Set the new target ratio to 16:9
|
233 |
+
target_width = 1280 # Adjust target width based on desired resolution
|
234 |
+
overlap = 48
|
235 |
+
fade_width = 24
|
236 |
+
max_height = 720 # Adjust max height instead of width
|
237 |
+
|
238 |
+
# Resize the image if it's taller than max_height
|
239 |
+
if source.height > max_height:
|
240 |
+
scale_factor = max_height / source.height
|
241 |
+
new_height = max_height
|
242 |
+
new_width = int(source.width * scale_factor)
|
243 |
+
source = source.resize((new_width, new_height), Image.LANCZOS)
|
244 |
+
|
245 |
+
# Calculate the required width for the 16:9 ratio
|
246 |
+
target_width = (source.height * target_ratio[0]) // target_ratio[1]
|
247 |
+
|
248 |
+
# Calculate margins (now left and right)
|
249 |
+
margin_x = (target_width - source.width) // 2
|
250 |
+
|
251 |
+
# Calculate new output size
|
252 |
+
output_size = (target_width, source.height)
|
253 |
+
|
254 |
+
# Create a white background
|
255 |
+
background = Image.new('RGB', output_size, (255, 255, 255))
|
256 |
+
|
257 |
+
# Calculate position to paste the original image
|
258 |
+
position = (margin_x, 0)
|
259 |
+
|
260 |
+
# Paste the original image onto the white background
|
261 |
+
background.paste(source, position)
|
262 |
+
|
263 |
+
# Create the mask
|
264 |
+
mask = Image.new('L', output_size, 255) # Start with all white
|
265 |
+
mask_draw = ImageDraw.Draw(mask)
|
266 |
+
mask_draw.rectangle([
|
267 |
+
(margin_x + overlap, overlap),
|
268 |
+
(margin_x + source.width - overlap, source.height - overlap)
|
269 |
+
], fill=0)
|
270 |
+
|
271 |
+
# Prepare the image for ControlNet
|
272 |
+
cnet_image = background.copy()
|
273 |
+
cnet_image.paste(0, (0, 0), mask)
|
274 |
+
|
275 |
+
for image in pipe(
|
276 |
+
prompt_embeds=prompt_embeds,
|
277 |
+
negative_prompt_embeds=negative_prompt_embeds,
|
278 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
279 |
+
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
|
280 |
+
image=cnet_image,
|
281 |
+
):
|
282 |
+
yield image, cnet_image
|
283 |
+
|
284 |
+
image = image.convert("RGBA")
|
285 |
+
cnet_image.paste(image, (0, 0), mask)
|
286 |
+
|
287 |
+
yield background, cnet_image
|
288 |
|
289 |
+
elif ratio_choice == "9:16":
|
290 |
+
|
291 |
+
target_ratio=(9, 16)
|
292 |
+
target_height=1280
|
293 |
+
overlap=48
|
294 |
+
fade_width=24
|
295 |
+
max_width = 720
|
296 |
+
# Resize the image if it's wider than max_width
|
297 |
+
if source.width > max_width:
|
298 |
+
scale_factor = max_width / source.width
|
299 |
+
new_width = max_width
|
300 |
+
new_height = int(source.height * scale_factor)
|
301 |
+
source = source.resize((new_width, new_height), Image.LANCZOS)
|
302 |
+
|
303 |
+
# Calculate the required height for 9:16 ratio
|
304 |
+
target_height = (source.width * target_ratio[1]) // target_ratio[0]
|
305 |
+
|
306 |
+
# Calculate margins (only top and bottom)
|
307 |
+
margin_y = (target_height - source.height) // 2
|
308 |
+
|
309 |
+
# Calculate new output size
|
310 |
+
output_size = (source.width, target_height)
|
311 |
+
|
312 |
+
# Create a white background
|
313 |
+
background = Image.new('RGB', output_size, (255, 255, 255))
|
314 |
+
|
315 |
+
# Calculate position to paste the original image
|
316 |
+
position = (0, margin_y)
|
317 |
+
|
318 |
+
# Paste the original image onto the white background
|
319 |
+
background.paste(source, position)
|
320 |
+
|
321 |
+
# Create the mask
|
322 |
+
mask = Image.new('L', output_size, 255) # Start with all white
|
323 |
+
mask_draw = ImageDraw.Draw(mask)
|
324 |
+
mask_draw.rectangle([
|
325 |
+
(overlap, margin_y + overlap),
|
326 |
+
(source.width - overlap, margin_y + source.height - overlap)
|
327 |
+
], fill=0)
|
328 |
+
|
329 |
+
# Prepare the image for ControlNet
|
330 |
+
cnet_image = background.copy()
|
331 |
+
cnet_image.paste(0, (0, 0), mask)
|
332 |
+
|
333 |
+
for image in pipe(
|
334 |
+
prompt_embeds=prompt_embeds,
|
335 |
+
negative_prompt_embeds=negative_prompt_embeds,
|
336 |
+
pooled_prompt_embeds=pooled_prompt_embeds,
|
337 |
+
negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
|
338 |
+
image=cnet_image,
|
339 |
+
):
|
340 |
+
yield image, cnet_image
|
341 |
+
|
342 |
+
image = image.convert("RGBA")
|
343 |
+
cnet_image.paste(image, (0, 0), mask)
|
344 |
+
|
345 |
+
yield background, cnet_image
|
346 |
|
347 |
|
348 |
def clear_result():
|
|
|
356 |
"""
|
357 |
|
358 |
|
359 |
+
title = """<h1 align="center">Diffusers Image Outpaint</h1>
|
360 |
+
<div align="center">Drop an image you would like to extend, pick your expected ratio and hit Generate.</div>
|
361 |
"""
|
362 |
|
363 |
with gr.Blocks(css=css) as demo:
|
|
|
376 |
interactive=False,
|
377 |
label="Generated Image",
|
378 |
)
|
379 |
+
|
380 |
+
with gr.Row():
|
381 |
+
ratio = gr.Radio(
|
382 |
+
label="Expected ratio",
|
383 |
+
choices=["9:16", "16:9"],
|
384 |
+
value = "9:16"
|
385 |
+
)
|
386 |
+
model_selection = gr.Dropdown(
|
387 |
+
choices=list(MODELS.keys()),
|
388 |
+
value="RealVisXL V5.0 Lightning",
|
389 |
+
label="Model",
|
390 |
+
)
|
391 |
|
392 |
run_button.click(
|
393 |
fn=clear_result,
|
|
|
395 |
outputs=result,
|
396 |
).then(
|
397 |
fn=fill_image,
|
398 |
+
inputs=[input_image, model_selection, ratio],
|
399 |
outputs=result,
|
400 |
)
|
401 |
|