0
# Command-Line Interface
1
2
Comprehensive CLI tools for batch image processing, folder monitoring, HTTP server functionality, and model management. The CLI provides a complete interface to rembg's capabilities without requiring Python programming.
3
4
## Installation
5
6
```bash
7
pip install rembg[cli]
8
```
9
10
## Capabilities
11
12
### Single Image Processing
13
14
Process individual image files with full control over model selection and processing options.
15
16
```bash { .api }
17
rembg i [OPTIONS] INPUT OUTPUT
18
19
Options:
20
-m, --model [u2net|u2netp|u2net_human_seg|u2net_cloth_seg|u2net_custom|birefnet_general|birefnet_general_lite|birefnet_portrait|birefnet_dis|birefnet_hrsod|birefnet_cod|birefnet_massive|dis_anime|dis_custom|dis_general_use|sam|silueta|bria_rmbg|ben_custom]
21
Model name (default: u2net)
22
-a, --alpha-matting Use alpha matting for higher quality
23
-af, --alpha-matting-foreground-threshold INTEGER
24
Trimap foreground threshold (0-255, default: 240)
25
-ab, --alpha-matting-background-threshold INTEGER
26
Trimap background threshold (0-255, default: 10)
27
-ae, --alpha-matting-erode-size INTEGER
28
Alpha matting erosion size (default: 10)
29
-om, --only-mask Return only the binary mask
30
-ppm, --post-process-mask Apply post-processing to mask
31
-bgc, --bgcolor "R G B A" Background color (space-separated, e.g., "255 255 255 255")
32
```
33
34
**Usage Examples:**
35
36
```bash
37
# Basic usage
38
rembg i input.jpg output.png
39
40
# Use specific model
41
rembg i -m birefnet_portrait portrait.jpg output.png
42
43
# High-quality with alpha matting
44
rembg i -a input.jpg output.png
45
46
# Get only the mask
47
rembg i -om input.jpg mask.png
48
49
# Custom background color (white)
50
rembg i -bgc "255 255 255 255" input.jpg output.jpg
51
```
52
53
### Folder Processing
54
55
Batch process entire folders of images with support for parallel processing and progress tracking.
56
57
```bash { .api }
58
rembg p [OPTIONS] INPUT_FOLDER OUTPUT_FOLDER
59
60
Options:
61
-m, --model [model_names...] Model name (default: u2net)
62
-a, --alpha-matting Use alpha matting
63
-af, --alpha-matting-foreground-threshold INTEGER (default: 240)
64
-ab, --alpha-matting-background-threshold INTEGER (default: 10)
65
-ae, --alpha-matting-erode-size INTEGER (default: 10)
66
-om, --only-mask Return only masks
67
-ppm, --post-process-mask Apply post-processing
68
-bgc, --bgcolor TEXT Background color
69
-w, --watch Watch folder for new files
70
-d, --delete_input Delete input files after processing
71
```
72
73
**Usage Examples:**
74
75
```bash
76
# Process entire folder
77
rembg p input_photos/ output_photos/
78
79
# Watch folder for new files
80
rembg p -w input_photos/ output_photos/
81
82
# Batch process with alpha matting
83
rembg p -a photos/ processed/
84
```
85
86
### Binary Stream Processing
87
88
Process RGB24 pixel data streams, typically from video processing pipelines like FFmpeg.
89
90
```bash { .api }
91
rembg b [OPTIONS] IMAGE_WIDTH IMAGE_HEIGHT
92
93
Arguments:
94
IMAGE_WIDTH Width of input images in pixels
95
IMAGE_HEIGHT Height of input images in pixels
96
97
Options:
98
-m, --model [model_names...] Model name (default: u2net)
99
-a, --alpha-matting Use alpha matting
100
-af, --alpha-matting-foreground-threshold INTEGER (default: 240)
101
-ab, --alpha-matting-background-threshold INTEGER (default: 10)
102
-ae, --alpha-matting-erode-size INTEGER (default: 10)
103
-om, --only-mask Return only masks
104
-ppm, --post-process-mask Apply post-processing
105
-bgc, --bgcolor "R G B A" Background color (space-separated)
106
-x, --extras TEXT Extra parameters as JSON
107
-o, --output_specifier TEXT Printf-style output filename pattern
108
```
109
110
**Usage Examples:**
111
112
```bash
113
# Process RGB24 stream from FFmpeg
114
ffmpeg -i input.mp4 -ss 10 -an -f rawvideo -pix_fmt rgb24 pipe:1 | rembg b 1280 720 -o output-%03u.png
115
116
# Process with specific model
117
ffmpeg -i video.mp4 -f rawvideo -pix_fmt rgb24 pipe:1 | rembg b -m birefnet_general 1920 1080 -o frame_%04d.png
118
119
# Output to stdout
120
ffmpeg -i video.mp4 -f rawvideo -pix_fmt rgb24 pipe:1 | rembg b 1920 1080 > processed_frames.bin
121
```
122
123
### HTTP Server
124
125
Run rembg as a web service with REST API endpoints for integration with other applications.
126
127
```bash { .api }
128
rembg s [OPTIONS]
129
130
Options:
131
--host TEXT Server host (default: 0.0.0.0)
132
--port INTEGER Server port (default: 7000)
133
--log_level [DEBUG|INFO|WARNING|ERROR|CRITICAL]
134
Logging level (default: INFO)
135
--threads INTEGER Number of worker threads
136
```
137
138
**Usage Examples:**
139
140
```bash
141
# Start server on default port 7000
142
rembg s
143
144
# Start server on custom port
145
rembg s --port 8080
146
147
# Start with debug logging
148
rembg s --log_level DEBUG
149
```
150
151
**API Endpoints:**
152
153
```bash
154
# Remove background from uploaded image
155
POST /api/remove
156
Content-Type: multipart/form-data
157
Body: file=<image_file>, model=<model_name>
158
159
# Health check
160
GET /api/health
161
162
# List available models
163
GET /api/models
164
```
165
166
### Model Download
167
168
Download and manage AI models locally for offline usage.
169
170
```bash { .api }
171
rembg d [MODEL_NAMES...]
172
173
Arguments:
174
MODEL_NAMES: Space-separated list of model names to download.
175
If none specified, downloads all available models.
176
177
Available models:
178
u2net, u2netp, u2net_human_seg, u2net_cloth_seg, u2net_custom,
179
birefnet_general, birefnet_general_lite, birefnet_portrait,
180
birefnet_dis, birefnet_hrsod, birefnet_cod, birefnet_massive,
181
dis_anime, dis_custom, dis_general_use, sam, silueta,
182
bria_rmbg, ben_custom
183
```
184
185
**Usage Examples:**
186
187
```bash
188
# Download specific models
189
rembg d u2net birefnet_portrait
190
191
# Download all models
192
rembg d
193
194
# Download model family
195
rembg d birefnet_general birefnet_general_lite birefnet_portrait
196
```
197
198
## Common Options
199
200
All processing commands support these common options:
201
202
### Model Selection
203
- **-m, --model**: Choose from 23 available AI models
204
- Each model is optimized for specific use cases (portraits, anime, general, etc.)
205
206
### Alpha Matting
207
- **-a, --alpha-matting**: Enable high-quality edge refinement
208
- **-af**: Foreground threshold (higher = more aggressive foreground detection)
209
- **-ab**: Background threshold (lower = more aggressive background detection)
210
- **-ae**: Erosion size (larger = smoother edges, may lose detail)
211
212
### Output Options
213
- **-om, --only-mask**: Output binary mask instead of cutout
214
- **-ppm, --post-process-mask**: Apply morphological smoothing to mask
215
- **-bgc, --bgcolor**: Apply solid background color (format: "R G B A")
216
217
## Performance Tips
218
219
### Batch Processing
220
- Use folder processing (`rembg p`) for multiple images
221
- Use binary stream processing (`rembg b`) for video pipelines
222
- Reuse models across images for better performance
223
- Consider using lighter models for large batches
224
225
### Model Selection
226
- **u2net**: Best balance for most use cases
227
- **birefnet_general_lite**: Faster alternative with good quality
228
- **dis_anime**: Much faster for anime/cartoon content
229
230
### Server Mode
231
- Use HTTP server for integration with web applications
232
- Server keeps models loaded in memory for faster response times
233
- Configure appropriate logging level for production use
234
235
## File Format Support
236
237
### Input Formats
238
- JPEG, PNG, TIFF, BMP, WebP
239
- Most common image formats supported via PIL
240
241
### Output Formats
242
- PNG (recommended for transparency)
243
- JPEG (with background color)
244
- TIFF, BMP, WebP
245
246
### Batch Processing
247
- Maintains original file structure
248
- Automatically handles mixed format folders
249
- Preserves metadata when possible