Spaces:
Running
on
Zero
Running
on
Zero
Update utils/image_utils.py
Browse files- utils/image_utils.py +7 -9
utils/image_utils.py
CHANGED
@@ -5,7 +5,7 @@ from rembg import remove
|
|
5 |
|
6 |
def background_removal(input_image_path):
|
7 |
"""
|
8 |
-
|
9 |
"""
|
10 |
try:
|
11 |
input_image = Image.open(input_image_path)
|
@@ -16,15 +16,13 @@ def background_removal(input_image_path):
|
|
16 |
# ่ๆฏ้คๅปๅฆ็
|
17 |
rgba_image = remove(input_image).convert("RGBA")
|
18 |
rgba_np = np.array(rgba_image)
|
19 |
-
alpha_channel = rgba_np[:, :, 3]
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
# ใขใซใใกใในใฏใไฝฟใฃใฆ่ๆฏ๏ผ็ฝ๏ผใ้้้จๅใซๅๆ
|
27 |
-
result = np.where(alpha_channel[:, :, None] == 0, background, foreground)
|
28 |
|
29 |
return Image.fromarray(result)
|
30 |
|
|
|
5 |
|
6 |
def background_removal(input_image_path):
|
7 |
"""
|
8 |
+
ๆๅฎใใใ็ปๅใใ่ๆฏใ้คๅปใใ้ๆ้จๅใ็ฝ่ๆฏใซใใฌใณใใใฆ่ฟใ้ขๆฐ
|
9 |
"""
|
10 |
try:
|
11 |
input_image = Image.open(input_image_path)
|
|
|
16 |
# ่ๆฏ้คๅปๅฆ็
|
17 |
rgba_image = remove(input_image).convert("RGBA")
|
18 |
rgba_np = np.array(rgba_image)
|
19 |
+
alpha_channel = rgba_np[:, :, 3] / 255.0 # ใขใซใใกใ0~1ใฎ็ฏๅฒใซๆญฃ่ฆๅ
|
20 |
|
21 |
+
# ็ฝ่ๆฏใฎ็ๆ
|
22 |
+
background = np.ones_like(rgba_np[:, :, :3], dtype=np.uint8) * 255 # ็ฝ่ๆฏ
|
23 |
+
|
24 |
+
# ใขใซใใกใใฌใณใใ่กใใ้ๆ้จๅใ็ฝใจๅๆ
|
25 |
+
result = (foreground * alpha_channel[:, :, None] + background * (1 - alpha_channel[:, :, None])).astype(np.uint8)
|
|
|
|
|
26 |
|
27 |
return Image.fromarray(result)
|
28 |
|