add hex to rgba, fix for bg color
Browse files
app.py
CHANGED
@@ -6,22 +6,31 @@ import logging
|
|
6 |
# Set up logging
|
7 |
logging.basicConfig(level=logging.INFO)
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
def remove_background(input_image, bg_color, model_name, alpha_matting, post_process_mask, only_mask):
|
10 |
try:
|
11 |
# Set up the session with the chosen model, or None if no model is selected
|
12 |
session = new_session(model_name) if model_name else None
|
13 |
|
|
|
|
|
|
|
14 |
# Prepare additional options
|
15 |
remove_kwargs = {
|
16 |
"session": session,
|
17 |
-
"bgcolor":
|
18 |
"alpha_matting": alpha_matting,
|
19 |
"post_process_mask": post_process_mask,
|
20 |
"only_mask": only_mask
|
21 |
}
|
22 |
|
23 |
# Use the remove function
|
24 |
-
if session:
|
25 |
output_image = remove(input_image, **{k: v for k, v in remove_kwargs.items() if v is not None})
|
26 |
else:
|
27 |
output_image = remove(input_image) # Use the default remove function
|
|
|
6 |
# Set up logging
|
7 |
logging.basicConfig(level=logging.INFO)
|
8 |
|
9 |
+
def hex_to_rgba(hex_color):
|
10 |
+
hex_color = hex_color.lstrip('#')
|
11 |
+
if len(hex_color) == 6:
|
12 |
+
hex_color += 'FF' # Add full opacity if no alpha is provided
|
13 |
+
return tuple(int(hex_color[i:i + 2], 16) for i in (0, 2, 4, 6))
|
14 |
+
|
15 |
def remove_background(input_image, bg_color, model_name, alpha_matting, post_process_mask, only_mask):
|
16 |
try:
|
17 |
# Set up the session with the chosen model, or None if no model is selected
|
18 |
session = new_session(model_name) if model_name else None
|
19 |
|
20 |
+
# Convert hex color to RGBA tuple
|
21 |
+
bg_color_rgba = hex_to_rgba(bg_color) if bg_color else None
|
22 |
+
|
23 |
# Prepare additional options
|
24 |
remove_kwargs = {
|
25 |
"session": session,
|
26 |
+
"bgcolor": bg_color_rgba if bg_color_rgba else None,
|
27 |
"alpha_matting": alpha_matting,
|
28 |
"post_process_mask": post_process_mask,
|
29 |
"only_mask": only_mask
|
30 |
}
|
31 |
|
32 |
# Use the remove function
|
33 |
+
if session or bg_color_rgba:
|
34 |
output_image = remove(input_image, **{k: v for k, v in remove_kwargs.items() if v is not None})
|
35 |
else:
|
36 |
output_image = remove(input_image) # Use the default remove function
|