fffiloni commited on
Commit
976671e
1 Parent(s): 9cdaf5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -148
app.py CHANGED
@@ -102,128 +102,9 @@ 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):
109
- source = image
110
- target_ratio=(9, 16)
111
- target_height=1280
112
- overlap=48
113
- fade_width=24
114
- max_width = 720
115
- # Resize the image if it's wider than max_width
116
- if source.width > max_width:
117
- scale_factor = max_width / source.width
118
- new_width = max_width
119
- new_height = int(source.height * scale_factor)
120
- source = source.resize((new_width, new_height), Image.LANCZOS)
121
-
122
- # Calculate the required height for 9:16 ratio
123
- target_height = (source.width * target_ratio[1]) // target_ratio[0]
124
-
125
- # Calculate margins (only top and bottom)
126
- margin_y = (target_height - source.height) // 2
127
-
128
- # Calculate new output size
129
- output_size = (source.width, target_height)
130
-
131
- # Create a white background
132
- background = Image.new('RGB', output_size, (255, 255, 255))
133
-
134
- # Calculate position to paste the original image
135
- position = (0, margin_y)
136
-
137
- # Paste the original image onto the white background
138
- background.paste(source, position)
139
-
140
- # Create the mask
141
- mask = Image.new('L', output_size, 255) # Start with all white
142
- mask_draw = ImageDraw.Draw(mask)
143
- mask_draw.rectangle([
144
- (overlap, margin_y + overlap),
145
- (source.width - overlap, margin_y + source.height - overlap)
146
- ], fill=0)
147
-
148
- # Prepare the image for ControlNet
149
- cnet_image = background.copy()
150
- cnet_image.paste(0, (0, 0), mask)
151
-
152
- for image in pipe(
153
- prompt_embeds=prompt_embeds,
154
- negative_prompt_embeds=negative_prompt_embeds,
155
- pooled_prompt_embeds=pooled_prompt_embeds,
156
- negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
157
- image=cnet_image,
158
- ):
159
- yield image, cnet_image
160
-
161
- image = image.convert("RGBA")
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 infer(image, model_selection, ratio_choice):
228
 
229
  source = image
@@ -361,40 +242,46 @@ title = """<h1 align="center">Diffusers Image Outpaint</h1>
361
  """
362
 
363
  with gr.Blocks(css=css) as demo:
364
- gr.HTML(title)
365
-
366
- run_button = gr.Button("Generate")
367
-
368
- with gr.Row():
369
- input_image = gr.Image(
370
- type="pil",
371
- label="Input Image",
372
- sources=["upload"],
373
- )
374
-
375
- result = ImageSlider(
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,
394
  inputs=None,
395
  outputs=result,
396
  ).then(
397
- fn=fill_image,
398
  inputs=[input_image, model_selection, ratio],
399
  outputs=result,
400
  )
 
102
  cnet_image.paste(image, (0, 0), mask)
103
 
104
  yield background, cnet_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  """
106
 
107
+ @spaces.GPU
108
  def infer(image, model_selection, ratio_choice):
109
 
110
  source = image
 
242
  """
243
 
244
  with gr.Blocks(css=css) as demo:
245
+ with gr.Column():
246
+
247
+ gr.HTML(title)
248
+
249
+ with gr.Row():
250
+
251
+ with gr.Column():
252
+
253
+ input_image = gr.Image(
254
+ type="pil",
255
+ label="Input Image",
256
+ sources=["upload"],
257
+ )
258
+
259
+ with gr.Row():
260
+ ratio = gr.Radio(
261
+ label="Expected ratio",
262
+ choices=["9:16", "16:9"],
263
+ value = "9:16"
264
+ )
265
+ model_selection = gr.Dropdown(
266
+ choices=list(MODELS.keys()),
267
+ value="RealVisXL V5.0 Lightning",
268
+ label="Model",
269
+ )
270
+
271
+ run_button = gr.Button("Generate")
272
+
273
+ with gr.Column():
274
+ result = ImageSlider(
275
+ interactive=False,
276
+ label="Generated Image",
277
+ )
278
 
279
  run_button.click(
280
  fn=clear_result,
281
  inputs=None,
282
  outputs=result,
283
  ).then(
284
+ fn=infer,
285
  inputs=[input_image, model_selection, ratio],
286
  outputs=result,
287
  )