How danielgatis/rembg works
Rembg removes image backgrounds through a Python API and several CLI modes: single-image, folder/batch, HTTP server, and RGB24 stream processing. The shared processing layer turns supported inputs into images, obtains a mask from a selected model session, optionally refines the mask or edges, and returns a cutout or mask. Model sessions cover local ONNX models and the optional withoutBG cloud backend. README documents these capabilities; sampled source confirms the core processing, session creation, server request handling, and selected model behaviors, but not every command’s implementation details.
Main entry points: bg.py Removal Pipeline · session_factory.py Session Factory · base.py Local Model Files · p_command.py Folder CLI
Components 21
User Interfaces
- Python API
rembg/__init__.py - Single Image CLI
rembg/commands/i_command.py - Folder CLI
rembg/commands/p_command.py - HTTP Server
rembg/commands/s_command.py - RGB24 CLI
rembg/commands/b_command.py - Model Download CLI
rembg/commands/d_command.py - Model Migration CLI
rembg/commands/m_command.py
Image Processing
- Removal Pipeline
rembg/bg.py - Alpha Matting
rembg/bg.py - ViTMatte Refinement
rembg/matting.py - Image or Mask Result
rembg/bg.py
Model Sessions
- Session Factory
rembg/session_factory.py - Model Registry
rembg/sessions/__init__.py - ONNX Session Base
rembg/sessions/base.py - Model Implementations
rembg/sessions - SAM Session
rembg/sessions/sam.py - withoutBG Session
rembg/sessions/withoutbg.py
Models and Services
- Local Model Files
rembg/sessions/base.py - Model Download Host
- withoutBG API
Other components
- User or Client
Connections 32
- User or Client → Python API: calls
- User or Client → Single Image CLI: runs
- User or Client → Folder CLI: runs
- User or Client → HTTP Server: requests
- User or Client → RGB24 CLI: streams pixels
- User or Client → Model Download CLI: requests downloads
- User or Client → Model Migration CLI: requests migration
- Python API → Removal Pipeline: calls
- Single Image CLI → Removal Pipeline: calls
- Folder CLI → Session Factory: creates session
- Folder CLI → Removal Pipeline: processes images
- HTTP Server → Session Factory: creates sessions
- HTTP Server → Removal Pipeline: processes requests
- RGB24 CLI → Session Factory: creates session
- RGB24 CLI → Removal Pipeline: processes frames
- Model Download CLI → Session Factory: creates sessions
- Model Migration CLI → Local Model Files: copies model files
- Removal Pipeline → Session Factory: creates session
- Removal Pipeline → Alpha Matting: refines mask
- Removal Pipeline → ViTMatte Refinement: refines edges
- Removal Pipeline → Image or Mask Result: returns cutout
- Session Factory → Model Registry: selects model
- Session Factory → Model Implementations: instantiates
- Model Implementations → ONNX Session Base: inherits session
- Model Implementations → Local Model Files: loads local weights
- SAM Session → Local Model Files: loads encoder weights
- SAM Session → Model Download Host: downloads weights
- ONNX Session Base → Local Model Files: loads weights
- ONNX Session Base → Model Download Host: downloads weights
- withoutBG Session → withoutBG API: sends image
- withoutBG Session → Removal Pipeline: returns mask
- Local Model Files → Model Migration CLI: receives migrated files