backgroundremover
Background Remover lets you Remove Background from images and video using AI with a simple command line interface that is free and open source.
Top Related Projects
Rembg is a tool to remove images background
Image inpainting tool powered by SOTA AI Model. Remove any unwanted object, defect, people from your pictures or erase and replace(powered by stable diffusion) any thing on your pictures.
Remove backgrounds from images directly in the browser environment with ease and no additional costs or privacy concerns. Explore an interactive demo.
Quick Overview
BackgroundRemover is an open-source Python library for removing backgrounds from images and videos. It utilizes machine learning models to automatically detect and separate foreground subjects from their backgrounds, offering both command-line and programmatic interfaces for easy integration into various workflows.
Pros
- Supports both image and video background removal
- Offers multiple AI models for different use cases and quality levels
- Provides both CLI and Python API for flexibility
- Free and open-source
Cons
- Dependency on large AI models may require significant disk space
- Processing speed can be slow, especially for high-resolution images or videos
- May struggle with complex or ambiguous backgrounds
- Limited customization options for fine-tuning results
Code Examples
- Removing background from an image:
from backgroundremover import remove
input_path = "input_image.jpg"
output_path = "output_image.png"
remove(input_path, output_path)
- Removing background from a video:
from backgroundremover import remove_video_background
input_video = "input_video.mp4"
output_video = "output_video.mp4"
remove_video_background(input_video, output_video)
- Using a specific AI model:
from backgroundremover import remove
input_path = "input_image.jpg"
output_path = "output_image.png"
model_name = "u2net"
remove(input_path, output_path, model_name=model_name)
Getting Started
- Install the library:
pip install backgroundremover
- Basic usage for image background removal:
from backgroundremover import remove
remove("input_image.jpg", "output_image.png")
- For video background removal:
from backgroundremover import remove_video_background
remove_video_background("input_video.mp4", "output_video.mp4")
Competitor Comparisons
Rembg is a tool to remove images background
Pros of rembg
- More actively maintained with frequent updates
- Supports both CLI and API usage
- Offers multiple model options for different use cases
Cons of rembg
- Larger package size due to additional dependencies
- Slightly more complex setup process
- May require more system resources for some operations
Code Comparison
rembg:
from rembg import remove
from PIL import Image
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)
backgroundremover:
from backgroundremover import remove
input_path = 'input.png'
output_path = 'output.png'
remove(input_path, output_path)
Both libraries offer straightforward usage for background removal, but rembg provides more flexibility with its API and model options. backgroundremover has a simpler interface but may lack some advanced features. The choice between the two depends on specific project requirements and the level of control needed over the background removal process.
Image inpainting tool powered by SOTA AI Model. Remove any unwanted object, defect, people from your pictures or erase and replace(powered by stable diffusion) any thing on your pictures.
Pros of IOPaint
- Offers a user-friendly GUI interface for image editing
- Provides more advanced inpainting and object removal features
- Supports real-time editing and interactive brushes
Cons of IOPaint
- Requires more system resources due to its GUI and advanced features
- May have a steeper learning curve for users unfamiliar with image editing tools
- Limited to local processing, potentially slower for batch operations
Code Comparison
BackgroundRemover:
from backgroundremover import remove
input_path = "input.jpg"
output_path = "output.png"
remove(input_path, output_path)
IOPaint:
from iopaint import inpaint
image = cv2.imread("input.jpg")
mask = cv2.imread("mask.png", cv2.IMREAD_GRAYSCALE)
result = inpaint(image, mask)
cv2.imwrite("output.png", result)
BackgroundRemover focuses on simple background removal with a straightforward API, while IOPaint offers more complex image manipulation capabilities through its inpainting functions. BackgroundRemover is better suited for quick, automated background removal tasks, whereas IOPaint provides more control and flexibility for detailed image editing and restoration.
Remove backgrounds from images directly in the browser environment with ease and no additional costs or privacy concerns. Explore an interactive demo.
Pros of background-removal-js
- Pure JavaScript implementation, making it easier to integrate into web applications
- Supports both browser and Node.js environments
- Offers a simple API for easy usage
Cons of background-removal-js
- Limited to image processing only, while backgroundremover supports both images and videos
- May have lower accuracy compared to backgroundremover's use of U2Net model
- Requires a larger file size due to bundled ML model
Code Comparison
backgroundremover:
from backgroundremover import remove
input_path = "input.jpg"
output_path = "output.png"
remove(input_path, output_path)
background-removal-js:
import { removeBackground } from 'background-removal-js';
const inputImage = document.getElementById('input-image');
const result = await removeBackground(inputImage);
document.body.appendChild(result);
Both libraries offer straightforward APIs for background removal, but backgroundremover uses Python while background-removal-js is JavaScript-based. The latter provides a more web-friendly approach, allowing direct manipulation of DOM elements. However, backgroundremover's Python implementation may offer more flexibility for server-side processing and integration with other image processing libraries.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
BackgroundRemover
BackgroundRemover is a command line tool to remove background from image and video using AI, made by nadermx to power https://BackgroundRemoverAI.com. If you wonder why it was made read this short blog post.
Requirements
-
python >= 3.6
-
python3.6-dev #or what ever version of python you use
-
torch and torchvision stable version (https://pytorch.org)
-
ffmpeg 4.4+
-
To clarify, you must install both python and whatever dev version of python you installed. IE; python3.10-dev with python3.10 or python3.8-dev with python3.8
How to install torch and ffmpeg
Go to https://pytorch.org and scroll down to INSTALL PYTORCH
section and follow the instructions.
For example:
PyTorch Build: Stable (1.7.1)
Your OS: Windows
Package: Pip
Language: Python
CUDA: None
To install ffmpeg and python-dev
sudo apt install ffmpeg python3.6-dev
Installation
To Install backgroundremover, install it from pypi
pip install --upgrade pip
pip install backgroundremover
Please note that when you first run the program, it will check to see if you have the u2net models, if you do not, it will pull them from this repo
It is also possible to run this without installing it via pip, just clone the git to local start a virtual env and install requirements and run
python -m backgroundremover.cmd.cli -i "video.mp4" -mk -o "output.mov"
and for windows
python.exe -m backgroundremover.cmd.cli -i "video.mp4" -mk -o "output.mov"
Installation using Docker
git clone https://github.com/nadermx/backgroundremover.git
cd backgroundremover
docker build -t bgremover .
alias backgroundremover='docker run -it --rm -v "$(pwd):/tmp" bgremover:latest'
Usage as a cli
Image
Remove the background from a local file image
backgroundremover -i "/path/to/image.jpeg" -o "output.png"
Advance usage for image background removal
Sometimes it is possible to achieve better results by turning on alpha matting. Example:
backgroundremover -i "/path/to/image.jpeg" -a -ae 15 -o "output.png"
change the model for different background removal methods between u2netp
, u2net
, or u2net_human_seg
backgroundremover -i "/path/to/image.jpeg" -m "u2net_human_seg" -o "output.png"
Video
remove background from video and make transparent mov
backgroundremover -i "/path/to/video.mp4" -tv -o "output.mov"
remove background from local video and overlay it over other video
backgroundremover -i "/path/to/video.mp4" -tov "/path/to/videtobeoverlayed.mp4" -o "output.mov"
remove background from local video and overlay it over an image
backgroundremover -i "/path/to/video.mp4" -toi "/path/to/videtobeoverlayed.mp4" -o "output.mov"
remove background from video and make transparent gif
backgroundremover -i "/path/to/video.mp4" -tg -o "output.gif"
Make matte key file (green screen overlay)
Make a matte file for premiere
backgroundremover -i "/path/to/video.mp4" -mk -o "output.matte.mp4"
Advance usage for video
Change the framerate of the video (default is set to 30)
backgroundremover -i "/path/to/video.mp4" -fr 30 -tv -o "output.mov"
Set total number of frames of the video (default is set to -1, ie the remove background from full video)
backgroundremover -i "/path/to/video.mp4" -fl 150 -tv -o "output.mov"
Change the gpu batch size of the video (default is set to 1)
backgroundremover -i "/path/to/video.mp4" -gb 4 -tv -o "output.mov"
Change the number of workers working on video (default is set to 1)
backgroundremover -i "/path/to/video.mp4" -wn 4 -tv -o "output.mov"
change the model for different background removal methods between u2netp
, u2net
, or u2net_human_seg
and limit the frames to 150
backgroundremover -i "/path/to/video.mp4" -m "u2net_human_seg" -fl 150 -tv -o "output.mov"
As a library
Remove background image
from backgroundremover.bg import remove
def remove_bg(src_img_path, out_img_path):
model_choices = ["u2net", "u2net_human_seg", "u2netp"]
f = open(src_img_path, "rb")
data = f.read()
img = remove(data, model_name=model_choices[0],
alpha_matting=True,
alpha_matting_foreground_threshold=240,
alpha_matting_background_threshold=10,
alpha_matting_erode_structure_size=10,
alpha_matting_base_size=1000)
f.close()
f = open(out_img_path, "wb")
f.write(img)
f.close()
Todo
- convert logic from video to image to utilize more GPU on image removal
- clean up documentation a bit more
- add ability to adjust and give feedback images or videos to datasets
- add ability to realtime background removal for videos, for streaming
- finish flask server api
- add ability to use other models than u2net, ie your own
- other
Pull requests
Accepted
If you like this library
Give a link to our project BackgroundRemoverAI.com or this git, telling people that you like it or use it.
Reason for project
We made it our own package after merging together parts of others, adding in a few features of our own via posting parts as bounty questions on superuser, etc. As well as asked on hackernews earlier to open source the image part, so decided to add in video, and a bit more.
References
- https://arxiv.org/pdf/2005.09007.pdf
- https://github.com/NathanUA/U-2-Net
- https://github.com/pymatting/pymatting
- https://github.com/danielgatis/rembg
- https://github.com/ecsplendid/rembg-greenscreen
- https://superuser.com/questions/1647590/have-ffmpeg-merge-a-matte-key-file-over-the-normal-video-file-removing-the-backg
- https://superuser.com/questions/1648680/ffmpeg-alphamerge-two-videos-into-a-gif-with-transparent-background/1649339?noredirect=1#comment2522687_1649339
- https://superuser.com/questions/1649817/ffmpeg-overlay-a-video-after-alphamerging-two-others/1649856#1649856
License
- Copyright (c) 2021-present Johnathan Nader
- Copyright (c) 2020-present Lucas Nestler
- Copyright (c) 2020-present Dr. Tim Scarfe
- Copyright (c) 2020-present Daniel Gatis
Code Licensed under MIT License Models Licensed under Apache License 2.0
Top Related Projects
Rembg is a tool to remove images background
Image inpainting tool powered by SOTA AI Model. Remove any unwanted object, defect, people from your pictures or erase and replace(powered by stable diffusion) any thing on your pictures.
Remove backgrounds from images directly in the browser environment with ease and no additional costs or privacy concerns. Explore an interactive demo.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot