Spaces:
Running
on
Zero
Running
on
Zero
tori29umai
commited on
Commit
โข
0a3b739
1
Parent(s):
906508c
Update utils/image_utils.py
Browse files- utils/image_utils.py +17 -5
utils/image_utils.py
CHANGED
@@ -5,18 +5,30 @@ 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)
|
12 |
except IOError:
|
13 |
print(f"Error: Cannot open {input_image_path}")
|
14 |
return None
|
15 |
|
16 |
-
# ่ๆฏ้คๅปๅฆ็
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
result_path = "tmp.png"
|
19 |
-
|
20 |
|
21 |
return result_path
|
22 |
|
|
|
5 |
|
6 |
def background_removal(input_image_path):
|
7 |
"""
|
8 |
+
ๆๅฎใใใ็ปๅใใ่ๆฏใ้คๅปใใ็ฝไปฅๅคใฎ้ ๅใงใใชใใณใฐใใ็ปๅใ่ฟใ้ขๆฐ
|
9 |
"""
|
10 |
try:
|
11 |
+
input_image = Image.open(input_image_path).convert("RGB")
|
12 |
except IOError:
|
13 |
print(f"Error: Cannot open {input_image_path}")
|
14 |
return None
|
15 |
|
16 |
+
# ่ๆฏ้คๅปๅฆ็ (็ฝ่ๆฏใฎRGB็ปๅใจใใฆ่ฟใใใ)
|
17 |
+
result_image = remove(input_image).convert("RGB")
|
18 |
+
|
19 |
+
# ใใชใใณใฐๅฆ็
|
20 |
+
bg = Image.new("RGB", result_image.size, (255, 255, 255)) # ็ฝ่ๆฏ
|
21 |
+
diff = ImageChops.difference(result_image, bg)
|
22 |
+
bbox = diff.getbbox() # ็ฝไปฅๅคใฎ้จๅใๅซใๅข็ใใใฏในใๅๅพ
|
23 |
+
|
24 |
+
if bbox:
|
25 |
+
cropped_image = result_image.crop(bbox)
|
26 |
+
else:
|
27 |
+
cropped_image = result_image # ๅ
จ้จใ็ฝใฎๅ ดๅใฏใใฎใพใพ่ฟใ
|
28 |
+
|
29 |
+
# ็ตๆใไฟๅญ
|
30 |
result_path = "tmp.png"
|
31 |
+
cropped_image.save(result_path)
|
32 |
|
33 |
return result_path
|
34 |
|