# danielgatis/rembg architecture

> Architecture diagram of the GitHub repository danielgatis/rembg, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/danielgatis/rembg
- Repository: https://github.com/danielgatis/rembg
- Video tour (about a minute): https://gitdiagram.com/danielgatis/rembg/video

## Overview

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: [`rembg/bg.py`](https://github.com/danielgatis/rembg/blob/main/rembg/bg.py) (Removal Pipeline), [`rembg/session_factory.py`](https://github.com/danielgatis/rembg/blob/main/rembg/session_factory.py) (Session Factory), [`rembg/sessions/base.py`](https://github.com/danielgatis/rembg/blob/main/rembg/sessions/base.py) (Local Model Files), [`rembg/commands/p_command.py`](https://github.com/danielgatis/rembg/blob/main/rembg/commands/p_command.py) (Folder CLI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entrypoints["User Interfaces"]
  node_library["Python API<br/>[__init__.py]"]
  node_single["Single Image CLI<br/>[i_command.py]"]
  node_batch["Folder CLI<br/>[p_command.py]"]
  node_server["HTTP Server<br/>[s_command.py]"]
  node_stream["RGB24 CLI<br/>[b_command.py]"]
  node_download["Model Download CLI<br/>[d_command.py]"]
  node_migration["Model Migration CLI<br/>[m_command.py]"]
end

subgraph group_processing["Image Processing"]
  node_remove["Removal Pipeline<br/>[bg.py]"]
  node_matting["Alpha Matting<br/>[bg.py]"]
  node_vitmatte["ViTMatte Refinement<br/>[matting.py]"]
  node_result["Image or Mask Result<br/>[bg.py]"]
end

subgraph group_models["Model Sessions"]
  node_factory["Session Factory<br/>[session_factory.py]"]
  node_registry["Model Registry<br/>[__init__.py]"]
  node_base["ONNX Session Base<br/>[base.py]"]
  node_modeltypes["Model Implementations"]
  node_sam["SAM Session<br/>[sam.py]"]
  node_withoutbg["withoutBG Session<br/>[withoutbg.py]"]
end

subgraph group_state["Models and Services"]
  node_modelstore[("Local Model Files<br/>[base.py]")]
  node_modelhost(("Model Download Host"))
  node_cloudapi(("withoutBG API"))
end

node_user(("User or Client"))

node_user -->|"calls"| node_library
node_user -->|"runs"| node_single
node_user -->|"runs"| node_batch
node_user -->|"requests"| node_server
node_user -->|"streams pixels"| node_stream
node_user -->|"requests downloads"| node_download
node_user -->|"requests migration"| node_migration
node_library -->|"calls"| node_remove
node_single -->|"calls"| node_remove
node_batch -->|"creates session"| node_factory
node_batch -->|"processes images"| node_remove
node_server -->|"creates sessions"| node_factory
node_server -->|"processes requests"| node_remove
node_stream -->|"creates session"| node_factory
node_stream -->|"processes frames"| node_remove
node_download -->|"creates sessions"| node_factory
node_migration -->|"copies model files"| node_modelstore
node_remove -->|"creates session"| node_factory
node_remove -->|"refines mask"| node_matting
node_remove -->|"refines edges"| node_vitmatte
node_remove -->|"returns cutout"| node_result
node_factory -->|"selects model"| node_registry
node_factory -->|"instantiates"| node_modeltypes
node_modeltypes -->|"inherits session"| node_base
node_modeltypes -->|"loads local weights"| node_modelstore
node_sam -->|"loads encoder weights"| node_modelstore
node_sam -->|"downloads weights"| node_modelhost
node_base -->|"loads weights"| node_modelstore
node_base -->|"downloads weights"| node_modelhost
node_withoutbg -.->|"sends image"| node_cloudapi
node_withoutbg -->|"returns mask"| node_remove
node_modelstore -->|"receives migrated files"| node_migration

click node_library "https://github.com/danielgatis/rembg/blob/main/rembg/__init__.py"
click node_single "https://github.com/danielgatis/rembg/blob/main/rembg/commands/i_command.py"
click node_batch "https://github.com/danielgatis/rembg/blob/main/rembg/commands/p_command.py"
click node_server "https://github.com/danielgatis/rembg/blob/main/rembg/commands/s_command.py"
click node_stream "https://github.com/danielgatis/rembg/blob/main/rembg/commands/b_command.py"
click node_download "https://github.com/danielgatis/rembg/blob/main/rembg/commands/d_command.py"
click node_migration "https://github.com/danielgatis/rembg/blob/main/rembg/commands/m_command.py"
click node_remove "https://github.com/danielgatis/rembg/blob/main/rembg/bg.py"
click node_matting "https://github.com/danielgatis/rembg/blob/main/rembg/bg.py"
click node_vitmatte "https://github.com/danielgatis/rembg/blob/main/rembg/matting.py"
click node_factory "https://github.com/danielgatis/rembg/blob/main/rembg/session_factory.py"
click node_registry "https://github.com/danielgatis/rembg/blob/main/rembg/sessions/__init__.py"
click node_base "https://github.com/danielgatis/rembg/blob/main/rembg/sessions/base.py"
click node_modeltypes "https://github.com/danielgatis/rembg/tree/main/rembg/sessions"
click node_sam "https://github.com/danielgatis/rembg/blob/main/rembg/sessions/sam.py"
click node_withoutbg "https://github.com/danielgatis/rembg/blob/main/rembg/sessions/withoutbg.py"
click node_modelstore "https://github.com/danielgatis/rembg/blob/main/rembg/sessions/base.py"
click node_result "https://github.com/danielgatis/rembg/blob/main/rembg/bg.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_library,node_single,node_batch,node_server,node_stream,node_download,node_migration,node_user toneBlue
class node_remove,node_matting,node_vitmatte,node_result toneAmber
class node_factory,node_registry,node_base,node_modeltypes,node_sam,node_withoutbg toneMint
class node_modelstore,node_modelhost,node_cloudapi toneRose
```

## Components

### User Interfaces

- [Python API](https://github.com/danielgatis/rembg/blob/main/rembg/__init__.py): `rembg/__init__.py`
- [Single Image CLI](https://github.com/danielgatis/rembg/blob/main/rembg/commands/i_command.py): `rembg/commands/i_command.py`
- [Folder CLI](https://github.com/danielgatis/rembg/blob/main/rembg/commands/p_command.py): `rembg/commands/p_command.py`
- [HTTP Server](https://github.com/danielgatis/rembg/blob/main/rembg/commands/s_command.py): `rembg/commands/s_command.py`
- [RGB24 CLI](https://github.com/danielgatis/rembg/blob/main/rembg/commands/b_command.py): `rembg/commands/b_command.py`
- [Model Download CLI](https://github.com/danielgatis/rembg/blob/main/rembg/commands/d_command.py): `rembg/commands/d_command.py`
- [Model Migration CLI](https://github.com/danielgatis/rembg/blob/main/rembg/commands/m_command.py): `rembg/commands/m_command.py`

### Image Processing

- [Removal Pipeline](https://github.com/danielgatis/rembg/blob/main/rembg/bg.py): `rembg/bg.py`
- [Alpha Matting](https://github.com/danielgatis/rembg/blob/main/rembg/bg.py): `rembg/bg.py`
- [ViTMatte Refinement](https://github.com/danielgatis/rembg/blob/main/rembg/matting.py): `rembg/matting.py`
- [Image or Mask Result](https://github.com/danielgatis/rembg/blob/main/rembg/bg.py): `rembg/bg.py`

### Model Sessions

- [Session Factory](https://github.com/danielgatis/rembg/blob/main/rembg/session_factory.py): `rembg/session_factory.py`
- [Model Registry](https://github.com/danielgatis/rembg/blob/main/rembg/sessions/__init__.py): `rembg/sessions/__init__.py`
- [ONNX Session Base](https://github.com/danielgatis/rembg/blob/main/rembg/sessions/base.py): `rembg/sessions/base.py`
- [Model Implementations](https://github.com/danielgatis/rembg/tree/main/rembg/sessions): `rembg/sessions`
- [SAM Session](https://github.com/danielgatis/rembg/blob/main/rembg/sessions/sam.py): `rembg/sessions/sam.py`
- [withoutBG Session](https://github.com/danielgatis/rembg/blob/main/rembg/sessions/withoutbg.py): `rembg/sessions/withoutbg.py`

### Models and Services

- [Local Model Files](https://github.com/danielgatis/rembg/blob/main/rembg/sessions/base.py): `rembg/sessions/base.py`
- **Model Download Host**
- **withoutBG API**

### Other components

- **User or Client**

## Connections

- 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
