AI-driven background removal — isolate the subject
Cuts the background out of any photo and keeps just the foreground subject — works on portraits, products, animals. Driven by an on-device AI model, no cloud calls, no upload, no per-image fee. Use THRESHOLD to fine-tune what counts as foreground when the AI is uncertain (e.g. wispy hair against a similar-colored backdrop).
INPUT
OUTPUT
JavaScript
// Tool plugin demo — alpha-cut background removal via U²-Net
// tool_rmbg.js
//!INPUT: INPUT
//!OUTPUT: OUTPUT
//!PARAM: THRESHOLD:number=0.0,min=0.0,max=1.0
// Demonstrates a Rust-based Tool Plugin with bundled neural-net
// weights. The 'rmbg' tool ships a small U²-Net ONNX model
// (~4.4 MB, Apache-2.0) and runs entirely in pure Rust via tract
//
// THRESHOLD pushes pixels below it in the raw mask to fully
// transparent. Leave at 0 for soft edges; raise to 0.4–0.5 for a
// cleaner cut-out at the cost of some hair / detail.
//
// First run takes a few seconds — the model is loaded from disk per
// invocation. Subsequent runs are equally slow (the engine spawns
// the binary fresh each time), but inference itself is ~5–10 s on
// a typical CPU for a 1000×1000 input.
const img = Engine.loadImage(INPUT);
const cut = Engine.tool('rmbg').apply(img, {
threshold: THRESHOLD,
});
cut.autocrop({ tolerance: 0.05, padding: 1 });
cut.save(OUTPUT);
img.free();
cut.free();
// © 2026 Michael Lechner · mlc OpticScript · https://mlcgo.eu · Elastic License 2.0