Installation
This library is for Python 3.9 only
CPU support:
pip install rembg
GPU support:
pip install rembg[gpu]
Usage as a CLI
Remove the background from a remote image:
curl -s http://input.png | rembg i > output.png
Remove the background from a local file:
rembg i path/to/input.png path/to/output.png
Remove the background from all images in a folder:
rembg p path/to/input path/to/output
Usage as a library
Input and output as bytes:
from rembg import remove
input_path = 'input.png'
output_path = 'output.png'
with open(input_path, 'rb') as i:
with open(output_path, 'wb') as o:
input = i.read()
output = remove(input)
o.write(output)
Input and output as a PIL image:
from rembg import remove
from PIL import Image
input_path = 'input.png'
output_path = 'output.png'
input = Image.open(input_path)
output = remove(input)
output.save(output_path)
Input and output as a NumPy array:
from rembg import remove
import cv2
input_path = 'input.png'
output_path = 'output.png'
input = cv2.imread(input_path)
output = remove(input)
cv2.imwrite(output_path, output)
Advanced usage
Sometimes it is possible to achieve better results by turning on alpha matting. Example:
curl -s http://input.png | rembg i -a -ae 15 > output.png