fffiloni commited on
Commit
4802ce5
1 Parent(s): d49f90c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -17
app.py CHANGED
@@ -119,11 +119,9 @@ def fill_image(image, model_selection):
119
  target_aspect = target_width / target_height
120
 
121
  if source_aspect > target_aspect:
122
- # Image is wider than target ratio, fit to width
123
  new_width = target_width
124
  new_height = int(new_width / source_aspect)
125
  else:
126
- # Image is taller than target ratio, fit to height
127
  new_height = target_height
128
  new_width = int(new_height * source_aspect)
129
 
@@ -140,25 +138,24 @@ def fill_image(image, model_selection):
140
  position = (margin_x, margin_y)
141
  background.paste(resized_source, position)
142
 
143
- # Create the mask with gradient edges
144
- mask = Image.new('L', (target_width, target_height), 255)
145
  mask_array = np.array(mask)
146
 
147
- # Create gradient for left and right edges
148
  for i in range(fade_width):
149
  alpha = i / fade_width
150
- mask_array[:, margin_x+overlap+i] = np.minimum(mask_array[:, margin_x+overlap+i], int(255 * (1 - alpha)))
151
- mask_array[:, margin_x+new_width-overlap-i-1] = np.minimum(mask_array[:, margin_x+new_width-overlap-i-1], int(255 * (1 - alpha)))
152
-
153
- # Create gradient for top and bottom edges
154
- for i in range(fade_width):
155
- alpha = i / fade_width
156
- mask_array[margin_y+overlap+i, :] = np.minimum(mask_array[margin_y+overlap+i, :], int(255 * (1 - alpha)))
157
- mask_array[margin_y+new_height-overlap-i-1, :] = np.minimum(mask_array[margin_y+new_height-overlap-i-1, :], int(255 * (1 - alpha)))
158
-
159
- # Set the center to black
160
- mask_array[margin_y+overlap+fade_width:margin_y+new_height-overlap-fade_width,
161
- margin_x+overlap+fade_width:margin_x+new_width-overlap-fade_width] = 0
162
 
163
  mask = Image.fromarray(mask_array.astype('uint8'), 'L')
164
 
 
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
 
 
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
+ mask_array[:, margin_x + new_width + i] = np.minimum(mask_array[:, margin_x + new_width + i], int(255 * alpha))
150
+ # Left edge
151
+ mask_array[:, margin_x - i - 1] = np.minimum(mask_array[:, margin_x - i - 1], int(255 * alpha))
152
+ # Bottom edge
153
+ mask_array[margin_y + new_height + i, :] = np.minimum(mask_array[margin_y + new_height + i, :], int(255 * alpha))
154
+ # Top edge
155
+ mask_array[margin_y - i - 1, :] = np.minimum(mask_array[margin_y - i - 1, :], int(255 * alpha))
156
+
157
+ # Set the area of the original image to black (0)
158
+ mask_array[margin_y:margin_y+new_height, margin_x:margin_x+new_width] = 0
 
159
 
160
  mask = Image.fromarray(mask_array.astype('uint8'), 'L')
161