danielgatis / rembg

Rembg is a tool to remove images background
MIT License
15.39k stars 1.75k forks source link

How to fix black border after remove bg? #606

Closed vuhaopro90 closed 2 months ago

vuhaopro90 commented 3 months ago

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)
Gowtham58 commented 3 months ago

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))
github-actions[bot] commented 2 months ago

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

github-actions[bot] commented 2 months ago

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