Spaces:
Sleeping
Sleeping
Abdullah-Habib
commited on
Commit
·
944593a
1
Parent(s):
85b1357
added image resizing
Browse files
app.py
CHANGED
@@ -42,7 +42,25 @@ def remove_bg(image: Image):
|
|
42 |
# Apply background removal using rembg
|
43 |
output_array_bg = rembg.remove(input_array_bg)
|
44 |
# Create a PIL Image from the output array
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
def update_selection(evt: gr.SelectData):
|
48 |
selected_lora = loras[evt.index]
|
|
|
42 |
# Apply background removal using rembg
|
43 |
output_array_bg = rembg.remove(input_array_bg)
|
44 |
# Create a PIL Image from the output array
|
45 |
+
img = Image.fromarray(output_array_bg)
|
46 |
+
|
47 |
+
mask = img.convert('L') # Convert to grayscale
|
48 |
+
mask_array = np.array(mask)
|
49 |
+
|
50 |
+
# Create a binary mask (non-background areas are 255, background areas are 0)
|
51 |
+
binary_mask = mask_array > 0
|
52 |
+
|
53 |
+
# Find the bounding box of the non-background areas
|
54 |
+
coords = np.argwhere(binary_mask)
|
55 |
+
x0, y0 = coords.min(axis=0)
|
56 |
+
x1, y1 = coords.max(axis=0) + 1
|
57 |
+
|
58 |
+
# Crop the output image using the bounding box
|
59 |
+
cropped_output_image = img.crop((y0, x0, y1, x1))
|
60 |
+
|
61 |
+
# Resize the cropped image to 1024x1024
|
62 |
+
upscaled_image = cropped_output_image.resize((1024, 1024), Image.LANCZOS)
|
63 |
+
return upscaled_image
|
64 |
|
65 |
def update_selection(evt: gr.SelectData):
|
66 |
selected_lora = loras[evt.index]
|