Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
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
|
144 |
-
mask = Image.new('L', (target_width, target_height), 255)
|
145 |
mask_array = np.array(mask)
|
146 |
|
147 |
-
# Create gradient
|
148 |
for i in range(fade_width):
|
149 |
alpha = i / fade_width
|
150 |
-
|
151 |
-
mask_array[:, margin_x+new_width
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
mask_array[margin_y
|
158 |
-
|
159 |
-
# Set the
|
160 |
-
mask_array[margin_y
|
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 |
|