Tyler Ng commited on
Update app.py
Browse filesfix transparent background
app.py
CHANGED
|
@@ -24,8 +24,19 @@ def process_image(input_image, output_type):
|
|
| 24 |
return None
|
| 25 |
|
| 26 |
if output_type == "Transparent Background (RGBA)":
|
| 27 |
-
#
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
elif output_type == "Green Background":
|
| 31 |
# Returns the masked image with green screen background
|
|
|
|
| 24 |
return None
|
| 25 |
|
| 26 |
if output_type == "Transparent Background (RGBA)":
|
| 27 |
+
# Get the mask first
|
| 28 |
+
mask = remover.process(input_image, type='map')
|
| 29 |
+
|
| 30 |
+
# Convert mask to proper format
|
| 31 |
+
if isinstance(mask, Image.Image):
|
| 32 |
+
mask = mask.convert('L')
|
| 33 |
+
else:
|
| 34 |
+
mask = Image.fromarray((mask * 255).astype(np.uint8), mode='L')
|
| 35 |
+
|
| 36 |
+
# Convert original image to RGBA and apply mask as alpha channel
|
| 37 |
+
img_rgba = input_image.convert('RGBA')
|
| 38 |
+
img_rgba.putalpha(mask)
|
| 39 |
+
output = img_rgba
|
| 40 |
|
| 41 |
elif output_type == "Green Background":
|
| 42 |
# Returns the masked image with green screen background
|