Skip to content

Remove-Background-ai/rembg-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RemBG CLI

A cross-platform command-line interface for batch background removal using the rembg.js library.

Features

  • 🚀 Batch Processing: Process single images or entire directories
  • 🌍 Cross-Platform: Works on Windows, macOS, and Linux
  • 🎨 Multiple Formats: Supports JPG, JPEG, PNG, and WEBP
  • 📊 Progress Tracking: Real-time progress indicators and detailed logging
  • Fast Processing: Optimized for speed and reliability
  • 🔧 Flexible Configuration: Environment variables and CLI options

Installation

Prerequisites

  • Node.js v18.17.0 or higher
  • A valid API key from RemBG.com

Setup

  1. Use the release binaries or Clone this repository
  2. Install dependencies:
    npm install
  3. Set up your API key:
    # Copy the example environment file
    cp env.example .env
    
    # Edit .env and add your API key
    X_API_KEY=your_actual_api_key_here

Usage

Basic Usage

# Process a single image
rembg-cli -i image.jpg -o ./output

# Process all images in a directory
rembg-cli -i ./images -o ./output

# Specify output format
rembg-cli -i ./images -o ./output -f png

Command Options

Option Short Description Default
--input -i Input file or directory Required
--output -o Output directory ./output
--format -f Output format (png, webp) webp
--verbose -v Enable verbose logging false
--api-key Override API key from environment From env
--version Show version number
--help Show help information

Examples

# Process a single PNG image
rembg-cli -i photo.png -o ./processed

# Process all images in a folder with verbose output
rembg-cli -i ./photos -o ./processed -v

# Process images and save as JPG
rembg-cli -i ./photos -o ./processed -f jpg

# Use a different API key for this session
rembg-cli -i ./photos -o ./processed --api-key your_key_here

Environment Variables

Create a .env file in the project root:

X_API_KEY=your_api_key_here
DEFAULT_OUTPUT_FORMAT=webp
DEFAULT_OUTPUT_DIR=./output

Building Executables

Build cross-platform executables using pkg:

# Build for Windows
npm run build:win

# Build for macOS
npm run build:mac

# Build for Linux
npm run build:linux

# Build for all platforms
npm run build:all

The built executables will be in the project root:

  • rembg-cli-win.exe (Windows)
  • rembg-cli-mac (macOS)
  • rembg-cli-linux (Linux)

Supported Formats

Input Formats

  • JPG/JPEG
  • PNG
  • WEBP

Output Formats

  • WEBP (default)
  • PNG

Error Handling

The CLI provides comprehensive error handling:

  • Validation: Checks for required parameters and file existence
  • 🔍 API Errors: Handles API failures gracefully
  • 📁 File System: Manages directory creation and file operations
  • 🚨 User Feedback: Clear error messages and suggestions

Performance Tips

  1. Batch Processing: Process multiple images in a single command for better efficiency
  2. Output Format: Use PNG for best quality, WEBP for smaller file sizes and perfect for most users
  3. API Limits: The tool processes images sequentially to respect API rate limits
  4. Storage: Ensure sufficient disk space for output files

Troubleshooting

Common Issues

"X_API_KEY is not set"

  • Set your API key in the .env file or use the --api-key option

"Input path does not exist"

  • Check that the file or directory path is correct
  • Use absolute paths if relative paths don't work

"No supported image files found"

  • Ensure your files have supported extensions (.jpg, .jpeg, .png, .webp)
  • Check that the files are not corrupted

"Failed to process image"

  • Verify your API key is valid and has sufficient credits
  • Check your internet connection
  • Try processing the image individually to isolate the issue

Getting Help

  • Use rembg-cli --help for command options
  • Use rembg-cli --version to check the version
  • Enable verbose mode with -v for detailed logging

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

Support

For API-related issues, visit RemBG Support page.