Skip to content

n8bird-oss/bgremover

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bgremove

Fast background removal CLI written in Rust. Two modes — pick the one that fits your situation:

Fast mode (default) Neural mode
Setup None — just build and run Download a ~170 MB model
Speed ~50–150 ms/image ~300–600 ms (CPU) / 40–100 ms (GPU)
Quality Good for typical photos Best — U2-Net deep learning
Deploy Single binary, zero downloads Binary + model file

Install

You need Rust installed. Then:

git clone https://github.com/n8bird-oss/bgremover
cd bgremover
cargo build --release

The binary will be at ./target/release/bgremove.


Two Modes

Fast mode (no setup needed)

./target/release/bgremove -i photo.jpg -o result.png

Neural mode (best quality)

Download the model once (~170 MB):

curl -L https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net.onnx -o u2net.onnx

Then run:

./target/release/bgremove -i photo.jpg -o result.png --model u2net.onnx

⚠️ Always use .png as output — JPEG does not support transparency (alpha channel).


All Options

Options:
  -i, --input <INPUT>          Input image path (PNG or JPEG)
  -o, --output <OUTPUT>        Output image path [default: output.png]
  -m, --model <MODEL>          U2-Net ONNX model (omit to use fast mode)
  -t, --threshold <THRESHOLD>  Mask threshold 0.0–1.0 [default: 0.5]
      --soft-edges             Soft-edge blending (better for hair/fur)
      --batch <BATCH>          Batch-process a directory of images
  -h, --help                   Print help
  -V, --version                Print version

Adjust threshold (-t)

Controls how aggressively the background is removed. Default is 0.5.

bgremove -i photo.jpg -o out.png -t 0.35   # looser — keeps more, good for hair/wispy edges
bgremove -i photo.jpg -o out.png -t 0.65   # tighter — cleaner edges, less background bleed

Soft edges

Edges fade naturally instead of a hard cut. Recommended for portraits and hair.

bgremove -i photo.jpg -o out.png --soft-edges

Batch process

bgremove --batch ./images/
# Outputs saved to ./images/removed/

Examples

# Basic removal
./target/release/bgremove -i portrait.jpg -o portrait.png

# Neural mode — best quality
./target/release/bgremove -i portrait.jpg -o portrait.png --model u2net.onnx

# Hair-friendly cutout
./target/release/bgremove -i portrait.jpg -o portrait.png --model u2net.onnx --soft-edges

# Tighter cutout
./target/release/bgremove -i portrait.jpg -o portrait.png -t 0.65

# Batch process a folder
./target/release/bgremove --batch ./photos/ --model u2net.onnx

How It Works

Fast mode — AuroraMatte

A zero-dependency algorithm that fuses three complementary signals:

1. Border color contrast
   Sample the outer 5% of pixels → build a background color model.
   Each pixel gets a score based on how different it is from that model.

2. Local texture saliency  (O(1)/pixel via integral images)
   Subjects (people, objects) have much richer local detail than
   plain walls, sky, or floors. Computed instantly using summed-area tables.

3. Center spatial prior
   A soft 2-D Gaussian centered on the image — photographers frame
   subjects centrally, so this boosts the center without hard-cutting edges.

Fusion → morphological close (fill holes) + open (remove noise) → Gaussian blur edges

All processing runs on a capped 640×640 resolution then upscales, so performance is flat regardless of input size.

Neural mode — U2-Net

Resize to 320×320 + normalize (ImageNet mean/std)
    │
    ▼
U2-Net ONNX inference  ──── CUDA if available, else CPU
    │
    ▼
Saliency mask [320×320 f32] → resize to original dimensions
    │
    ▼
Apply as alpha channel (hard threshold or soft blend)
    │
    ▼
Save as RGBA PNG

Use in Node.js

import { execFile } from "child_process";
import { promisify } from "util";

const run = promisify(execFile);

async function removeBackground(inputPath, outputPath, options = {}) {
  const args = ["-i", inputPath, "-o", outputPath];

  if (options.model) args.push("--model", options.model);
  if (options.threshold) args.push("-t", String(options.threshold));
  if (options.softEdges) args.push("--soft-edges");

  await run("./bgremove", args);
  return outputPath;
}

// Example
await removeBackground("photo.jpg", "result.png", {
  model: "./u2net.onnx",
  softEdges: true,
});

Alternative Neural Models

Download any from rembg releases:

Model Quality Speed Size Best for
u2net.onnx ★★★★★ Medium 170 MB General use
u2netp.onnx ★★★☆☆ Fast 5 MB Mobile / low-res
isnet.onnx ★★★★★ Slower 170 MB High-detail cutouts

Star History

Star History Chart


Contributing

Bug reports and pull requests are welcome. For significant changes, open an issue first.


License

MIT — see LICENSE.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages