tori29umai commited on
Commit
0aa210d
โ€ข
1 Parent(s): 05d9ddd

Update utils/image_utils.py

Browse files
Files changed (1) hide show
  1. utils/image_utils.py +22 -10
utils/image_utils.py CHANGED
@@ -3,16 +3,28 @@ import numpy as np
3
  import cv2
4
  from rembg import remove
5
 
6
- def remove_background(input_image):
7
- rgba_image = remove(input_image)
8
- # ใ‚ขใƒซใƒ•ใ‚กใƒใƒฃใƒใƒซใ‚’ใƒžใ‚นใ‚ฏใจใ—ใฆไฝฟ็”จ
9
- alpha_channel = rgba_image[:, :, 3]
10
-
11
- # ็™ฝใ„่ƒŒๆ™ฏ็”ปๅƒใ‚’ไฝœๆˆ
12
- background = np.ones_like(rgba_image, dtype=np.uint8) * 255
13
- # ใƒžใ‚นใ‚ฏใ‚’้ฉ็”จ
14
- background_masked = cv2.bitwise_and(background, background, mask=alpha_channel)
15
- return background_masked
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  def resize_image_aspect_ratio(image):
18
  # ๅ…ƒใฎ็”ปๅƒใ‚ตใ‚คใ‚บใ‚’ๅ–ๅพ—
 
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)
12
+ except IOError:
13
+ print(f"Error: Cannot open {input_image_path}")
14
+ return None
15
+
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
+ background = np.ones_like(rgba_np[:, :, :3], dtype=np.uint8) * 255
23
+ foreground = cv2.bitwise_and(rgba_np[:, :, :3], rgba_np[:, :, :3], mask=alpha_channel)
24
+ background_masked = cv2.bitwise_and(background, background, mask=cv2.bitwise_not(alpha_channel))
25
+ result = cv2.add(foreground, background_masked)
26
+
27
+ return Image.fromarray(result)
28
 
29
  def resize_image_aspect_ratio(image):
30
  # ๅ…ƒใฎ็”ปๅƒใ‚ตใ‚คใ‚บใ‚’ๅ–ๅพ—