Create model.py
Browse files
model.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
from io import BytesIO
|
3 |
+
from PIL import Image
|
4 |
+
import time
|
5 |
+
|
6 |
+
# Import your utility functions
|
7 |
+
from src.utils import change_background, matte
|
8 |
+
|
9 |
+
class BackgroundRemover:
|
10 |
+
def __init__(self):
|
11 |
+
self.hexmap = {
|
12 |
+
"Transparent (PNG)": "#000000",
|
13 |
+
"Black": "#000000",
|
14 |
+
"White": "#FFFFFF",
|
15 |
+
"Green": "#22EE22",
|
16 |
+
"Red": "#EE2222",
|
17 |
+
"Blue": "#2222EE",
|
18 |
+
}
|
19 |
+
|
20 |
+
def process_image(self, image_base64, background_color):
|
21 |
+
img_input = Image.open(BytesIO(base64.b64decode(image_base64)))
|
22 |
+
alpha = 0.0 if background_color == "Transparent (PNG)" else 1.0
|
23 |
+
img_matte = matte(img_input)
|
24 |
+
img_output = change_background(img_input, img_matte, background_alpha=alpha, background_hex=self.hexmap[background_color])
|
25 |
+
|
26 |
+
buffered = BytesIO()
|
27 |
+
img_output.save(buffered, format="PNG")
|
28 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
29 |
+
|
30 |
+
return img_str
|
31 |
+
|
32 |
+
model = BackgroundRemover()
|