multimodalart HF staff commited on
Commit
e5a75ef
·
verified ·
1 Parent(s): b784b02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -52
app.py CHANGED
@@ -47,42 +47,23 @@ pipe = StableDiffusionXLFillPipeline.from_pretrained(
47
 
48
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
49
 
50
- def resize_and_pad(image, target_size, resize_width=512):
51
- # Calculate the new height to maintain aspect ratio
52
- aspect_ratio = image.height / image.width
53
- new_height = int(resize_width * aspect_ratio)
54
-
55
- # Resize the image
56
- resized_image = image.resize((resize_width, new_height), Image.LANCZOS)
57
-
58
- # Create a new white image with the target size
59
- new_image = Image.new('RGB', target_size, (255, 255, 255))
60
-
61
- # Calculate position to paste the resized image (center it)
62
- paste_x = (target_size[0] - resize_width) // 2
63
- paste_y = (target_size[1] - new_height) // 2
64
-
65
- # Paste the resized image onto the new image
66
- new_image.paste(resized_image, (paste_x, paste_y))
67
-
68
- # Create a mask
69
- mask = Image.new('L', target_size, 255)
70
- mask_draw = ImageDraw.Draw(mask)
71
- mask_draw.rectangle([paste_x, paste_y, paste_x + resize_width, paste_y + new_height], fill=0)
72
-
73
- return new_image, mask
74
 
75
  @spaces.GPU
76
- def infer(image, model_selection, width, height, overlap_width, num_inference_steps, prompt_input=None, expand_mode=False):
 
77
  target_size = (width, height)
78
-
79
- if expand_mode:
80
- background, mask = resize_and_pad(image, target_size, resize_width=512)
81
- cnet_image = background.copy()
82
- cnet_image.paste(0, (0, 0), mask)
83
- else:
84
- source = image
85
-
 
 
 
 
86
  if source.width < target_size[0] and source.height < target_size[1]:
87
  scale_factor = min(target_size[0] / source.width, target_size[1] / source.height)
88
  new_width = int(source.width * scale_factor)
@@ -95,24 +76,24 @@ def infer(image, model_selection, width, height, overlap_width, num_inference_st
95
  new_height = int(source.height * scale_factor)
96
  source = source.resize((new_width, new_height), Image.LANCZOS)
97
 
98
- margin_x = (target_size[0] - source.width) // 2
99
- margin_y = (target_size[1] - source.height) // 2
100
 
101
- background = Image.new('RGB', target_size, (255, 255, 255))
102
- background.paste(source, (margin_x, margin_y))
103
 
104
- mask = Image.new('L', target_size, 255)
105
- mask_draw = ImageDraw.Draw(mask)
106
- mask_draw.rectangle([
107
- (margin_x + overlap_width, margin_y + overlap_width),
108
- (margin_x + source.width - overlap_width, margin_y + source.height - overlap_width)
109
- ], fill=0)
110
 
111
- cnet_image = background.copy()
112
- cnet_image.paste(0, (0, 0), mask)
113
 
114
  final_prompt = "high quality"
115
- if prompt_input and prompt_input.strip() != "":
116
  final_prompt += ", " + prompt_input
117
 
118
  (
@@ -139,21 +120,27 @@ def infer(image, model_selection, width, height, overlap_width, num_inference_st
139
 
140
  def preload_presets(target_ratio):
141
  if target_ratio == "9:16":
142
- return 720, 1280, gr.update(open=False)
 
 
143
  elif target_ratio == "16:9":
144
- return 1280, 720, gr.update(open=False)
 
 
145
  elif target_ratio == "Custom":
146
  return 720, 1280, gr.update(open=True)
147
 
148
  def clear_result():
149
  return gr.update(value=None)
150
 
 
151
  css = """
152
  .gradio-container {
153
  width: 1200px !important;
154
  }
155
  """
156
 
 
157
  title = """<h1 align="center">Diffusers Image Outpaint</h1>
158
  <div align="center">Drop an image you would like to extend, pick your expected ratio and hit Generate.</div>
159
  <div style="display: flex; justify-content: center; align-items: center; text-align: center;">
@@ -223,7 +210,11 @@ with gr.Blocks(css=css) as demo:
223
  step=1
224
  )
225
 
226
- expand_mode = gr.Checkbox(label="Use Expand Mode", value=False)
 
 
 
 
227
 
228
  gr.Examples(
229
  examples=[
@@ -246,14 +237,13 @@ with gr.Blocks(css=css) as demo:
246
  outputs = [width_slider, height_slider, settings_panel],
247
  queue = False
248
  )
249
-
250
  run_button.click(
251
  fn=clear_result,
252
  inputs=None,
253
  outputs=result,
254
  ).then(
255
  fn=infer,
256
- inputs=[input_image, model_selection, width_slider, height_slider, overlap_width, num_inference_steps, prompt_input, expand_mode],
257
  outputs=result,
258
  )
259
 
@@ -263,7 +253,7 @@ with gr.Blocks(css=css) as demo:
263
  outputs=result,
264
  ).then(
265
  fn=infer,
266
- inputs=[input_image, model_selection, width_slider, height_slider, overlap_width, num_inference_steps, prompt_input, expand_mode],
267
  outputs=result,
268
  )
269
 
 
47
 
48
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  @spaces.GPU
52
+ def infer(image, model_selection, width, height, overlap_width, num_inference_steps, resize_size, prompt_input=None):
53
+ source = image
54
  target_size = (width, height)
55
+ target_ratio = (width, height)
56
+ overlap = overlap_width
57
+
58
+ # New resizing logic based on resize_size
59
+ if resize_size != -1:
60
+ # Calculate new height to maintain aspect ratio
61
+ aspect_ratio = source.height / source.width
62
+ new_height = int(resize_size * aspect_ratio)
63
+ source = source.resize((resize_size, new_height), Image.LANCZOS)
64
+
65
+ # Existing resizing logic (now only applied if resize_size is -1)
66
+ if resize_size == -1:
67
  if source.width < target_size[0] and source.height < target_size[1]:
68
  scale_factor = min(target_size[0] / source.width, target_size[1] / source.height)
69
  new_width = int(source.width * scale_factor)
 
76
  new_height = int(source.height * scale_factor)
77
  source = source.resize((new_width, new_height), Image.LANCZOS)
78
 
79
+ margin_x = (target_size[0] - source.width) // 2
80
+ margin_y = (target_size[1] - source.height) // 2
81
 
82
+ background = Image.new('RGB', target_size, (255, 255, 255))
83
+ background.paste(source, (margin_x, margin_y))
84
 
85
+ mask = Image.new('L', target_size, 255)
86
+ mask_draw = ImageDraw.Draw(mask)
87
+ mask_draw.rectangle([
88
+ (margin_x + overlap, margin_y + overlap),
89
+ (margin_x + source.width - overlap, margin_y + source.height - overlap)
90
+ ], fill=0)
91
 
92
+ cnet_image = background.copy()
93
+ cnet_image.paste(0, (0, 0), mask)
94
 
95
  final_prompt = "high quality"
96
+ if prompt_input.strip() != "":
97
  final_prompt += ", " + prompt_input
98
 
99
  (
 
120
 
121
  def preload_presets(target_ratio):
122
  if target_ratio == "9:16":
123
+ changed_width = 720
124
+ changed_height = 1280
125
+ return changed_width, changed_height, gr.update(open=False)
126
  elif target_ratio == "16:9":
127
+ changed_width = 1280
128
+ changed_height = 720
129
+ return changed_width, changed_height, gr.update(open=False)
130
  elif target_ratio == "Custom":
131
  return 720, 1280, gr.update(open=True)
132
 
133
  def clear_result():
134
  return gr.update(value=None)
135
 
136
+
137
  css = """
138
  .gradio-container {
139
  width: 1200px !important;
140
  }
141
  """
142
 
143
+
144
  title = """<h1 align="center">Diffusers Image Outpaint</h1>
145
  <div align="center">Drop an image you would like to extend, pick your expected ratio and hit Generate.</div>
146
  <div style="display: flex; justify-content: center; align-items: center; text-align: center;">
 
210
  step=1
211
  )
212
 
213
+ resize_size = gr.Number(
214
+ label="Resize Size (-1 for default behavior)",
215
+ value=-1,
216
+ precision=0
217
+ )
218
 
219
  gr.Examples(
220
  examples=[
 
237
  outputs = [width_slider, height_slider, settings_panel],
238
  queue = False
239
  )
 
240
  run_button.click(
241
  fn=clear_result,
242
  inputs=None,
243
  outputs=result,
244
  ).then(
245
  fn=infer,
246
+ inputs=[input_image, model_selection, width_slider, height_slider, overlap_width, num_inference_steps, resize_size, prompt_input],
247
  outputs=result,
248
  )
249
 
 
253
  outputs=result,
254
  ).then(
255
  fn=infer,
256
+ inputs=[input_image, model_selection, width_slider, height_slider, overlap_width, num_inference_steps, resize_size, prompt_input],
257
  outputs=result,
258
  )
259