rembg icon indicating copy to clipboard operation
rembg copied to clipboard

How to fix black border after remove bg?

Open vuhaopro90 opened this issue 1 year ago • 1 comments

After remove bg I have a proplem, border is black like this [ Screenshot 2024-03-13 160352 how to fix it my code:

from rembg import remove
from PIL import Image, ImageDraw

# Đường dẫn ảnh gốc
input_path = 'du an part 2/DSC04113.jpg'

# Đường dẫn ảnh sau khi loại bỏ nền
output_path = 'du an part 2/goutput.png'

# Đường dẫn ảnh với nền mới
output_with_background_path = 'du an part 2/output.png'

# Màu nền mới (RGB)
new_background_color = (54, 113, 157)  # Màu 36719d

# Loại bỏ nền của ảnh
input_image = Image.open(input_path)
output_image = remove(input_image)


# Tạo ảnh mới với nền màu mới
background_image = Image.new('RGB', output_image.size, new_background_color)
background_image.paste(output_image, (0, 0), output_image)
background_image.save(output_with_background_path)

vuhaopro90 avatar Mar 13 '24 09:03 vuhaopro90

Hi, @vuhaopro90 try setting alpha_matting parameter to True, like this,

output_image = remove(input_image, alpha_matting=True)

and also you can directly get an image with color background using bgcolor,

output_image = remove(input_image, alpha_matting=True, bgcolor=(54, 113, 157, 255))

Gowtham58 avatar Mar 16 '24 16:03 Gowtham58

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Apr 16 '24 01:04 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Apr 30 '24 01:04 github-actions[bot]