fffiloni commited on
Commit
a7d6beb
1 Parent(s): c9f9263

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -35
app.py CHANGED
@@ -111,53 +111,32 @@ def fill_image(image, model_selection):
111
  target_height=1280
112
  overlap=48
113
  fade_width=24
114
- # Calculate target dimensions
115
- target_width = (target_height * target_ratio[0]) // target_ratio[1]
116
 
117
- # Resize the source image to fit the target width
118
- new_width = target_width
119
- new_height = int(source.height * (new_width / source.width))
120
- resized_source = source.resize((new_width, new_height), Image.LANCZOS)
 
121
 
122
  # Create a white background
123
- background = Image.new('RGB', (target_width, target_height), (255, 255, 255))
124
 
125
- # Calculate position to paste the resized image (centered vertically)
126
- margin_y = (target_height - new_height) // 2
127
  position = (0, margin_y)
128
 
129
- # Paste the resized image onto the white background
130
- background.paste(resized_source, position)
131
 
132
  # Create the mask
133
- mask = Image.new('L', (target_width, target_height), 255) # Start with all white
134
  mask_draw = ImageDraw.Draw(mask)
135
-
136
- # Draw black rectangle for the resized image area (with overlap)
137
  mask_draw.rectangle([
138
- (-overlap, margin_y - overlap),
139
- (new_width + overlap, margin_y + new_height + overlap)
140
  ], fill=0)
141
 
142
- # Convert mask to numpy array for gradient creation
143
- mask_array = np.array(mask)
144
-
145
- # Create gradient on the edges that overlap with the image
146
- for i in range(fade_width):
147
- alpha = i / fade_width
148
- # Top edge
149
- if margin_y - overlap + i < margin_y:
150
- mask_array[margin_y - overlap + i, :new_width] = int(255 * alpha)
151
- # Bottom edge
152
- if margin_y + new_height + overlap - i - 1 >= margin_y + new_height:
153
- mask_array[margin_y + new_height + overlap - i - 1, :new_width] = int(255 * alpha)
154
- # Left edge
155
- mask_array[margin_y:margin_y+new_height, i] = int(255 * alpha)
156
- # Right edge
157
- mask_array[margin_y:margin_y+new_height, new_width - i - 1] = int(255 * alpha)
158
-
159
- mask = Image.fromarray(mask_array.astype('uint8'), 'L')
160
-
161
  # Prepare the image for ControlNet
162
  cnet_image = background.copy()
163
  cnet_image.paste(0, (0, 0), mask)
 
111
  target_height=1280
112
  overlap=48
113
  fade_width=24
114
+ # Calculate the required height for 9:16 ratio
115
+ target_height = (source.width * target_ratio[1]) // target_ratio[0]
116
 
117
+ # Calculate margins (only top and bottom)
118
+ margin_y = (target_height - source.height) // 2
119
+
120
+ # Calculate new output size
121
+ output_size = (source.width, target_height)
122
 
123
  # Create a white background
124
+ background = Image.new('RGB', output_size, (255, 255, 255))
125
 
126
+ # Calculate position to paste the original image
 
127
  position = (0, margin_y)
128
 
129
+ # Paste the original image onto the white background
130
+ background.paste(source, position)
131
 
132
  # Create the mask
133
+ mask = Image.new('L', output_size, 255) # Start with all white
134
  mask_draw = ImageDraw.Draw(mask)
 
 
135
  mask_draw.rectangle([
136
+ (overlap, margin_y + overlap),
137
+ (source.width - overlap, margin_y + source.height - overlap)
138
  ], fill=0)
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  # Prepare the image for ControlNet
141
  cnet_image = background.copy()
142
  cnet_image.paste(0, (0, 0), mask)