KingNish commited on
Commit
cb28aaa
1 Parent(s): 639cf35

Added 1:1 image option.

Browse files
Files changed (1) hide show
  1. app.py +45 -51
app.py CHANGED
@@ -57,53 +57,6 @@ prompt = "high quality"
57
 
58
 
59
 
60
- """
61
- def fill_image(image, model_selection):
62
-
63
- margin = 256
64
- overlap = 24
65
- # Open the original image
66
- source = image # Changed from image["background"] to match new input format
67
-
68
- # Calculate new output size
69
- output_size = (source.width + 2*margin, source.height + 2*margin)
70
-
71
- # Create a white background
72
- background = Image.new('RGB', output_size, (255, 255, 255))
73
-
74
- # Calculate position to paste the original image
75
- position = (margin, margin)
76
-
77
- # Paste the original image onto the white background
78
- background.paste(source, position)
79
-
80
- # Create the mask
81
- mask = Image.new('L', output_size, 255) # Start with all white
82
- mask_draw = ImageDraw.Draw(mask)
83
- mask_draw.rectangle([
84
- (position[0] + overlap, position[1] + overlap),
85
- (position[0] + source.width - overlap, position[1] + source.height - overlap)
86
- ], fill=0)
87
-
88
- # Prepare the image for ControlNet
89
- cnet_image = background.copy()
90
- cnet_image.paste(0, (0, 0), mask)
91
-
92
- for image in pipe(
93
- prompt_embeds=prompt_embeds,
94
- negative_prompt_embeds=negative_prompt_embeds,
95
- pooled_prompt_embeds=pooled_prompt_embeds,
96
- negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
97
- image=cnet_image,
98
- ):
99
- yield image, cnet_image
100
-
101
- image = image.convert("RGBA")
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, overlap_width):
109
 
@@ -224,6 +177,47 @@ def infer(image, model_selection, ratio_choice, overlap_width):
224
  cnet_image.paste(image, (0, 0), mask)
225
 
226
  yield background, cnet_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
 
228
 
229
  def clear_result():
@@ -259,7 +253,7 @@ with gr.Blocks(css=css) as demo:
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(
@@ -280,9 +274,9 @@ with gr.Blocks(css=css) as demo:
280
 
281
  gr.Examples(
282
  examples = [
283
- ["./examples/example_1.webp", "RealVisXL V5.0 Lightning", "16:9"],
284
  ["./examples/example_2.jpg", "RealVisXL V5.0 Lightning", "16:9"],
285
- ["./examples/example_3.jpg", "RealVisXL V5.0 Lightning", "9:16"]
286
  ],
287
  inputs = [input_image, model_selection, ratio]
288
  )
@@ -304,4 +298,4 @@ with gr.Blocks(css=css) as demo:
304
  )
305
 
306
 
307
- demo.launch(share=False)
 
57
 
58
 
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  @spaces.GPU
61
  def infer(image, model_selection, ratio_choice, overlap_width):
62
 
 
177
  cnet_image.paste(image, (0, 0), mask)
178
 
179
  yield background, cnet_image
180
+
181
+ elif ratio_choice == "1:1":
182
+ target_ratio = (1, 1)
183
+ target_size = (1024, 1024)
184
+ overlap = overlap_width
185
+
186
+ if source.width > target_size[0] or source.height > target_size[1]:
187
+ scale_factor = min(target_size[0] / source.width, target_size[1] / source.height)
188
+ new_width = int(source.width * scale_factor)
189
+ new_height = int(source.height * scale_factor)
190
+ source = source.resize((new_width, new_height), Image.LANCZOS)
191
+
192
+ margin_x = (target_size[0] - source.width) // 2
193
+ margin_y = (target_size[1] - source.height) // 2
194
+
195
+ background = Image.new('RGB', target_size, (255, 255, 255))
196
+ background.paste(source, (margin_x, margin_y))
197
+
198
+ mask = Image.new('L', target_size, 255)
199
+ mask_draw = ImageDraw.Draw(mask)
200
+ mask_draw.rectangle([
201
+ (margin_x + overlap, margin_y + overlap),
202
+ (margin_x + source.width - overlap, margin_y + source.height - overlap)
203
+ ], fill=0)
204
+
205
+ cnet_image = background.copy()
206
+ cnet_image.paste(0, (0, 0), mask)
207
+
208
+ for image in pipe(
209
+ prompt_embeds=prompt_embeds,
210
+ negative_prompt_embeds=negative_prompt_embeds,
211
+ pooled_prompt_embeds=pooled_prompt_embeds,
212
+ negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
213
+ image=cnet_image,
214
+ ):
215
+ yield cnet_image, image
216
+
217
+ image = image.convert("RGBA")
218
+ cnet_image.paste(image, (0, 0), mask)
219
+
220
+ yield background, cnet_image
221
 
222
 
223
  def clear_result():
 
253
  with gr.Row():
254
  ratio = gr.Radio(
255
  label="Expected ratio",
256
+ choices=["1:1", "9:16", "16:9"],
257
  value = "9:16"
258
  )
259
  model_selection = gr.Dropdown(
 
274
 
275
  gr.Examples(
276
  examples = [
277
+ ["./examples/example_1.webp", "RealVisXL V5.0 Lightning", "9:16"],
278
  ["./examples/example_2.jpg", "RealVisXL V5.0 Lightning", "16:9"],
279
+ ["./examples/example_3.jpg", "RealVisXL V5.0 Lightning", "1:1"]
280
  ],
281
  inputs = [input_image, model_selection, ratio]
282
  )
 
298
  )
299
 
300
 
301
+ demo.launch(share=False)