fffiloni commited on
Commit
c01d116
1 Parent(s): 88590fc

custom resize option uses percentages instead of fractions

Browse files
Files changed (1) hide show
  1. app.py +31 -24
app.py CHANGED
@@ -53,7 +53,7 @@ def can_expand(source_width, source_height, target_width, target_height, alignme
53
  return True
54
 
55
  @spaces.GPU(duration=24)
56
- def infer(image, width, height, overlap_width, num_inference_steps, resize_option, custom_resize_size, prompt_input=None, alignment="Middle"):
57
  source = image
58
  target_size = (width, height)
59
  overlap = overlap_width
@@ -63,24 +63,31 @@ def infer(image, width, height, overlap_width, num_inference_steps, resize_optio
63
  new_width = int(source.width * scale_factor)
64
  new_height = int(source.height * scale_factor)
65
 
66
- # Resize the source image
67
  source = source.resize((new_width, new_height), Image.LANCZOS)
68
 
69
- # Apply resize option
70
  if resize_option == "Full":
71
- resize_size = max(source.width, source.height)
72
- elif resize_option == "1/2":
73
- resize_size = max(source.width, source.height) // 2
74
- elif resize_option == "1/3":
75
- resize_size = max(source.width, source.height) // 3
76
- elif resize_option == "1/4":
77
- resize_size = max(source.width, source.height) // 4
78
  else: # Custom
79
- resize_size = custom_resize_size
80
 
81
- aspect_ratio = source.height / source.width
82
- new_width = resize_size
83
- new_height = int(resize_size * aspect_ratio)
 
 
 
 
 
 
 
84
  source = source.resize((new_width, new_height), Image.LANCZOS)
85
 
86
  # Calculate margins based on alignment
@@ -283,15 +290,15 @@ with gr.Blocks(css=css) as demo:
283
  with gr.Row():
284
  resize_option = gr.Radio(
285
  label="Resize input image",
286
- choices=["Full", "1/2", "1/3", "1/4", "Custom"],
287
  value="Full"
288
  )
289
- custom_resize_size = gr.Slider(
290
- label="Custom resize size",
291
- minimum=64,
292
- maximum=1024,
293
- step=8,
294
- value=512,
295
  visible=False
296
  )
297
 
@@ -350,7 +357,7 @@ with gr.Blocks(css=css) as demo:
350
  resize_option.change(
351
  fn=toggle_custom_resize_slider,
352
  inputs=[resize_option],
353
- outputs=[custom_resize_size],
354
  queue=False
355
  )
356
 
@@ -361,7 +368,7 @@ with gr.Blocks(css=css) as demo:
361
  ).then( # Generate the new image
362
  fn=infer,
363
  inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,
364
- resize_option, custom_resize_size, prompt_input, alignment_dropdown],
365
  outputs=result,
366
  ).then( # Update the history gallery
367
  fn=lambda x, history: update_history(x[1], history),
@@ -380,7 +387,7 @@ with gr.Blocks(css=css) as demo:
380
  ).then( # Generate the new image
381
  fn=infer,
382
  inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,
383
- resize_option, custom_resize_size, prompt_input, alignment_dropdown],
384
  outputs=result,
385
  ).then( # Update the history gallery
386
  fn=lambda x, history: update_history(x[1], history),
 
53
  return True
54
 
55
  @spaces.GPU(duration=24)
56
+ def infer(image, width, height, overlap_width, num_inference_steps, resize_option, custom_resize_percentage, prompt_input=None, alignment="Middle"):
57
  source = image
58
  target_size = (width, height)
59
  overlap = overlap_width
 
63
  new_width = int(source.width * scale_factor)
64
  new_height = int(source.height * scale_factor)
65
 
66
+ # Resize the source image to fit within target size
67
  source = source.resize((new_width, new_height), Image.LANCZOS)
68
 
69
+ # Apply resize option using percentages
70
  if resize_option == "Full":
71
+ resize_percentage = 100
72
+ elif resize_option == "50%":
73
+ resize_percentage = 50
74
+ elif resize_option == "33%":
75
+ resize_percentage = 33
76
+ elif resize_option == "25%":
77
+ resize_percentage = 25
78
  else: # Custom
79
+ resize_percentage = custom_resize_percentage
80
 
81
+ # Calculate new dimensions based on percentage
82
+ resize_factor = resize_percentage / 100
83
+ new_width = int(source.width * resize_factor)
84
+ new_height = int(source.height * resize_factor)
85
+
86
+ # Ensure minimum size of 64 pixels
87
+ new_width = max(new_width, 64)
88
+ new_height = max(new_height, 64)
89
+
90
+ # Resize the image
91
  source = source.resize((new_width, new_height), Image.LANCZOS)
92
 
93
  # Calculate margins based on alignment
 
290
  with gr.Row():
291
  resize_option = gr.Radio(
292
  label="Resize input image",
293
+ choices=["Full", "50%", "33%", "25%", "Custom"],
294
  value="Full"
295
  )
296
+ custom_resize_percentage = gr.Slider(
297
+ label="Custom resize percentage",
298
+ minimum=1,
299
+ maximum=100,
300
+ step=1,
301
+ value=50,
302
  visible=False
303
  )
304
 
 
357
  resize_option.change(
358
  fn=toggle_custom_resize_slider,
359
  inputs=[resize_option],
360
+ outputs=[custom_resize_percentage],
361
  queue=False
362
  )
363
 
 
368
  ).then( # Generate the new image
369
  fn=infer,
370
  inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,
371
+ resize_option, custom_resize_percentage, prompt_input, alignment_dropdown],
372
  outputs=result,
373
  ).then( # Update the history gallery
374
  fn=lambda x, history: update_history(x[1], history),
 
387
  ).then( # Generate the new image
388
  fn=infer,
389
  inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,
390
+ resize_option, custom_resize_percentage, prompt_input, alignment_dropdown],
391
  outputs=result,
392
  ).then( # Update the history gallery
393
  fn=lambda x, history: update_history(x[1], history),