CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-rembg

Remove image background using advanced AI models including U-Net, BiRefNet, and SAM with support for multiple input formats and GPU acceleration

84

0.94x
Overview
Eval results
Files

cli.mddocs/

Command-Line Interface

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.

Installation

pip install rembg[cli]

Capabilities

Single Image Processing

Process individual image files with full control over model selection and processing options.

rembg i [OPTIONS] INPUT OUTPUT

Options:
  -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]
                                  Model name (default: u2net)
  -a, --alpha-matting             Use alpha matting for higher quality
  -af, --alpha-matting-foreground-threshold INTEGER
                                  Trimap foreground threshold (0-255, default: 240)
  -ab, --alpha-matting-background-threshold INTEGER  
                                  Trimap background threshold (0-255, default: 10)
  -ae, --alpha-matting-erode-size INTEGER
                                  Alpha matting erosion size (default: 10)
  -om, --only-mask               Return only the binary mask
  -ppm, --post-process-mask      Apply post-processing to mask
  -bgc, --bgcolor "R G B A"      Background color (space-separated, e.g., "255 255 255 255")

Usage Examples:

# Basic usage
rembg i input.jpg output.png

# Use specific model
rembg i -m birefnet_portrait portrait.jpg output.png

# High-quality with alpha matting
rembg i -a input.jpg output.png

# Get only the mask
rembg i -om input.jpg mask.png

# Custom background color (white)
rembg i -bgc "255 255 255 255" input.jpg output.jpg

Folder Processing

Batch process entire folders of images with support for parallel processing and progress tracking.

rembg p [OPTIONS] INPUT_FOLDER OUTPUT_FOLDER

Options:
  -m, --model [model_names...]    Model name (default: u2net)
  -a, --alpha-matting             Use alpha matting
  -af, --alpha-matting-foreground-threshold INTEGER (default: 240)
  -ab, --alpha-matting-background-threshold INTEGER (default: 10)
  -ae, --alpha-matting-erode-size INTEGER (default: 10)
  -om, --only-mask               Return only masks
  -ppm, --post-process-mask      Apply post-processing
  -bgc, --bgcolor TEXT           Background color
  -w, --watch                    Watch folder for new files
  -d, --delete_input             Delete input files after processing

Usage Examples:

# Process entire folder
rembg p input_photos/ output_photos/

# Watch folder for new files
rembg p -w input_photos/ output_photos/

# Batch process with alpha matting
rembg p -a photos/ processed/

Binary Stream Processing

Process RGB24 pixel data streams, typically from video processing pipelines like FFmpeg.

rembg b [OPTIONS] IMAGE_WIDTH IMAGE_HEIGHT

Arguments:
  IMAGE_WIDTH                    Width of input images in pixels
  IMAGE_HEIGHT                   Height of input images in pixels

Options:
  -m, --model [model_names...]   Model name (default: u2net)
  -a, --alpha-matting            Use alpha matting
  -af, --alpha-matting-foreground-threshold INTEGER (default: 240)
  -ab, --alpha-matting-background-threshold INTEGER (default: 10)
  -ae, --alpha-matting-erode-size INTEGER (default: 10)
  -om, --only-mask              Return only masks
  -ppm, --post-process-mask     Apply post-processing
  -bgc, --bgcolor "R G B A"     Background color (space-separated)
  -x, --extras TEXT             Extra parameters as JSON
  -o, --output_specifier TEXT   Printf-style output filename pattern

Usage Examples:

# Process RGB24 stream from FFmpeg
ffmpeg -i input.mp4 -ss 10 -an -f rawvideo -pix_fmt rgb24 pipe:1 | rembg b 1280 720 -o output-%03u.png

# Process with specific model
ffmpeg -i video.mp4 -f rawvideo -pix_fmt rgb24 pipe:1 | rembg b -m birefnet_general 1920 1080 -o frame_%04d.png

# Output to stdout
ffmpeg -i video.mp4 -f rawvideo -pix_fmt rgb24 pipe:1 | rembg b 1920 1080 > processed_frames.bin

HTTP Server

Run rembg as a web service with REST API endpoints for integration with other applications.

rembg s [OPTIONS]

Options:
  --host TEXT                    Server host (default: 0.0.0.0)
  --port INTEGER                 Server port (default: 7000)
  --log_level [DEBUG|INFO|WARNING|ERROR|CRITICAL]
                                 Logging level (default: INFO)
  --threads INTEGER              Number of worker threads

Usage Examples:

# Start server on default port 7000
rembg s

# Start server on custom port
rembg s --port 8080

# Start with debug logging
rembg s --log_level DEBUG

API Endpoints:

# Remove background from uploaded image
POST /api/remove
Content-Type: multipart/form-data
Body: file=<image_file>, model=<model_name>

# Health check
GET /api/health

# List available models
GET /api/models

Model Download

Download and manage AI models locally for offline usage.

rembg d [MODEL_NAMES...]

Arguments:
  MODEL_NAMES: Space-separated list of model names to download.
               If none specified, downloads all available models.

Available models:
  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

Usage Examples:

# Download specific models
rembg d u2net birefnet_portrait

# Download all models
rembg d

# Download model family
rembg d birefnet_general birefnet_general_lite birefnet_portrait

Common Options

All processing commands support these common options:

Model Selection

  • -m, --model: Choose from 23 available AI models
  • Each model is optimized for specific use cases (portraits, anime, general, etc.)

Alpha Matting

  • -a, --alpha-matting: Enable high-quality edge refinement
  • -af: Foreground threshold (higher = more aggressive foreground detection)
  • -ab: Background threshold (lower = more aggressive background detection)
  • -ae: Erosion size (larger = smoother edges, may lose detail)

Output Options

  • -om, --only-mask: Output binary mask instead of cutout
  • -ppm, --post-process-mask: Apply morphological smoothing to mask
  • -bgc, --bgcolor: Apply solid background color (format: "R G B A")

Performance Tips

Batch Processing

  • Use folder processing (rembg p) for multiple images
  • Use binary stream processing (rembg b) for video pipelines
  • Reuse models across images for better performance
  • Consider using lighter models for large batches

Model Selection

  • u2net: Best balance for most use cases
  • birefnet_general_lite: Faster alternative with good quality
  • dis_anime: Much faster for anime/cartoon content

Server Mode

  • Use HTTP server for integration with web applications
  • Server keeps models loaded in memory for faster response times
  • Configure appropriate logging level for production use

File Format Support

Input Formats

  • JPEG, PNG, TIFF, BMP, WebP
  • Most common image formats supported via PIL

Output Formats

  • PNG (recommended for transparency)
  • JPEG (with background color)
  • TIFF, BMP, WebP

Batch Processing

  • Maintains original file structure
  • Automatically handles mixed format folders
  • Preserves metadata when possible

Install with Tessl CLI

npx tessl i tessl/pypi-rembg

docs

background-removal.md

cli.md

index.md

session-management.md

utilities.md

tile.json