Spaces:
Running
on
Zero
Running
on
Zero
tori29umai
commited on
Commit
โข
16a8ae4
1
Parent(s):
62d9833
Update utils/image_utils.py
Browse files- utils/image_utils.py +12 -16
utils/image_utils.py
CHANGED
@@ -1,35 +1,31 @@
|
|
1 |
-
from PIL import Image
|
2 |
import numpy as np
|
3 |
import cv2
|
4 |
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).convert("
|
12 |
except IOError:
|
13 |
print(f"Error: Cannot open {input_image_path}")
|
14 |
return None
|
15 |
|
16 |
-
# ่ๆฏ้คๅปๅฆ็
|
17 |
-
|
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 |
-
|
26 |
-
else:
|
27 |
-
cropped_image = result_image # ๅ
จ้จใ็ฝใฎๅ ดๅใฏใใฎใพใพ่ฟใ
|
28 |
|
29 |
-
#
|
30 |
result_path = "tmp.png"
|
31 |
-
|
32 |
-
|
33 |
return result_path
|
34 |
|
35 |
def resize_image_aspect_ratio(image):
|
|
|
1 |
+
from PIL import Image
|
2 |
import numpy as np
|
3 |
import cv2
|
4 |
from rembg import remove
|
5 |
|
6 |
def background_removal(input_image_path):
|
7 |
"""
|
8 |
+
ๆๅฎใใใ็ปๅใใ่ๆฏใ้คๅปใใ้ๆ้จๅใ็ฝ่ๆฏใซใใฌใณใใใฆ่ฟใ้ขๆฐใ
|
9 |
+
้้ใใใฆใใชใ้จๅใงใใชใใณใฐใ่กใใพใใ
|
10 |
"""
|
11 |
try:
|
12 |
+
input_image = Image.open(input_image_path).convert("RGBA")
|
13 |
except IOError:
|
14 |
print(f"Error: Cannot open {input_image_path}")
|
15 |
return None
|
16 |
|
17 |
+
# ่ๆฏ้คๅปๅฆ็
|
18 |
+
result = remove(input_image)
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
# ้้ใใใฆใใชใ้จๅใใใชใใณใฐ
|
21 |
+
bbox = result.getbbox()
|
22 |
if bbox:
|
23 |
+
result = result.crop(bbox)
|
|
|
|
|
24 |
|
25 |
+
# ็ตๆใไธๆใใกใคใซใซไฟๅญ
|
26 |
result_path = "tmp.png"
|
27 |
+
result.save(result_path)
|
28 |
+
|
29 |
return result_path
|
30 |
|
31 |
def resize_image_aspect_ratio(image):
|