tori29umai commited on
Commit
1630a37
ยท
verified ยท
1 Parent(s): d35c5df

Update utils/image_utils.py

Browse files
Files changed (1) hide show
  1. 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
- foreground = rgba_np[:, :, :3] # RGB้ƒจๅˆ†ใฎใฟๅ–ๅพ—
23
- background = np.ones_like(foreground, dtype=np.uint8) * 255 # ็™ฝ่ƒŒๆ™ฏ
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