fffiloni commited on
Commit
6e4f1a9
1 Parent(s): 49f2888

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py CHANGED
@@ -56,6 +56,7 @@ prompt = "high quality"
56
 
57
 
58
  @spaces.GPU
 
59
  def fill_image(image, model_selection):
60
 
61
  margin = 256
@@ -100,7 +101,56 @@ def fill_image(image, model_selection):
100
  cnet_image.paste(image, (0, 0), mask)
101
 
102
  yield background, cnet_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
 
 
 
 
104
 
105
  def clear_result():
106
  return gr.update(value=None)
 
56
 
57
 
58
  @spaces.GPU
59
+ """
60
  def fill_image(image, model_selection):
61
 
62
  margin = 256
 
101
  cnet_image.paste(image, (0, 0), mask)
102
 
103
  yield background, cnet_image
104
+ """
105
+
106
+ def fill_image(image, model_selection):
107
+ source = image
108
+ target_ratio=(9, 16)
109
+ overlap=24
110
+ # Calculate the target width based on the 9:16 ratio
111
+ target_width = (source.height * target_ratio[0]) // target_ratio[1]
112
+
113
+ # Calculate margins
114
+ margin_x = max(0, (target_width - source.width) // 2)
115
+ margin_y = 0 # No vertical expansion
116
+
117
+ # Calculate new output size
118
+ output_size = (source.width + 2*margin_x, source.height + 2*margin_y)
119
+
120
+ # Create a white background
121
+ background = Image.new('RGB', output_size, (255, 255, 255))
122
+
123
+ # Calculate position to paste the original image
124
+ position = (margin_x, margin_y)
125
+
126
+ # Paste the original image onto the white background
127
+ background.paste(source, position)
128
+
129
+ # Create the mask
130
+ mask = Image.new('L', output_size, 255) # Start with all white
131
+ mask_draw = ImageDraw.Draw(mask)
132
+ mask_draw.rectangle([
133
+ (position[0] + overlap, position[1] + overlap),
134
+ (position[0] + source.width - overlap, position[1] + source.height - overlap)
135
+ ], fill=0)
136
+
137
+ # Prepare the image for ControlNet
138
+ cnet_image = background.copy()
139
+ cnet_image.paste(0, (0, 0), mask)
140
+
141
+ for image in pipe(
142
+ prompt_embeds=prompt_embeds,
143
+ negative_prompt_embeds=negative_prompt_embeds,
144
+ pooled_prompt_embeds=pooled_prompt_embeds,
145
+ negative_pooled_prompt_embeds=negative_pooled_prompt_embeds,
146
+ image=cnet_image,
147
+ ):
148
+ yield image, cnet_image
149
 
150
+ image = image.convert("RGBA")
151
+ cnet_image.paste(image, (0, 0), mask)
152
+
153
+ yield background, cnet_image
154
 
155
  def clear_result():
156
  return gr.update(value=None)