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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -31
app.py CHANGED
@@ -114,52 +114,47 @@ def fill_image(image, model_selection):
114
  # Calculate target dimensions
115
  target_width = (target_height * target_ratio[0]) // target_ratio[1]
116
 
117
- # Resize the source image to fit within the target dimensions while maintaining aspect ratio
118
- source_aspect = source.width / source.height
119
- target_aspect = target_width / target_height
120
-
121
- if source_aspect > target_aspect:
122
- new_width = target_width
123
- new_height = int(new_width / source_aspect)
124
- else:
125
- new_height = target_height
126
- new_width = int(new_height * source_aspect)
127
-
128
  resized_source = source.resize((new_width, new_height), Image.LANCZOS)
129
 
130
- # Calculate margins
131
- margin_x = (target_width - new_width) // 2
132
- margin_y = (target_height - new_height) // 2
133
-
134
  # Create a white background
135
  background = Image.new('RGB', (target_width, target_height), (255, 255, 255))
136
 
 
 
 
 
137
  # Paste the resized image onto the white background
138
- position = (margin_x, margin_y)
139
  background.paste(resized_source, position)
140
 
141
  # Create the mask
142
  mask = Image.new('L', (target_width, target_height), 255) # Start with all white
 
 
 
 
 
 
 
 
 
143
  mask_array = np.array(mask)
144
 
145
- # Create gradient only at the edges adjacent to the original image
146
  for i in range(fade_width):
147
  alpha = i / fade_width
148
- # Right edge
149
- if margin_x + new_width + i < target_width:
150
- mask_array[:, margin_x + new_width + i] = np.minimum(mask_array[:, margin_x + new_width + i], int(255 * alpha))
151
- # Left edge
152
- if margin_x - i - 1 >= 0:
153
- mask_array[:, margin_x - i - 1] = np.minimum(mask_array[:, margin_x - i - 1], int(255 * alpha))
154
- # Bottom edge
155
- if margin_y + new_height + i < target_height:
156
- mask_array[margin_y + new_height + i, :] = np.minimum(mask_array[margin_y + new_height + i, :], int(255 * alpha))
157
  # Top edge
158
- if margin_y - i - 1 >= 0:
159
- mask_array[margin_y - i - 1, :] = np.minimum(mask_array[margin_y - i - 1, :], int(255 * alpha))
160
-
161
- # Set the area of the original image to black (0)
162
- mask_array[margin_y:margin_y+new_height, margin_x:margin_x+new_width] = 0
 
 
 
 
163
 
164
  mask = Image.fromarray(mask_array.astype('uint8'), 'L')
165
 
 
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