| Crates.io | rembg-rs |
| lib.rs | rembg-rs |
| version | 0.1.4 |
| created_at | 2025-10-23 01:09:24.725908+00 |
| updated_at | 2025-12-28 01:01:47.697128+00 |
| description | A Rust library for removing backgrounds from images using neural networks |
| homepage | |
| repository | https://github.com/WarRaft/rembg-rs |
| max_upload_size | |
| id | 1896457 |
| size | 106,558 |
A Rust library for removing backgrounds from images using neural networks (ONNX Runtime + U2-Net).
ort crate)Add to your Cargo.toml:
[dependencies]
rembg = { git = "https://github.com/WarRaft/rembg-rs" }
[dependencies]
rembg = { git = "https://github.com/WarRaft/rembg-rs", features = ["cli"] }
Or build from source:
cargo build --release --features cli
use rembg::{Rembg, RemovalOptions};
use image::open;
use std::path::Path;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new background remover - OS manages memory
let mut rembg = Rembg::new(Path::new("models/u2net.onnx"))?;
// Load image
let img = open("input.jpg")?;
// Configure removal options
let options = RemovalOptions::default()
.with_threshold(0.5)
.with_binary_mode(false);
// Remove background
let result = rembg.remove_background(img, options)?;
// Get the result as raw image data
let (image, mask) = result.into_parts();
// Now you can use image and mask
// (save to file, send over network, etc.)
Ok(())
}
use rembg::{Rembg, RemovalOptions};
use image::load_from_memory;
use std::path::Path;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize once at bot startup - OS manages memory automatically
let mut rembg = Rembg::new(Path::new("models/u2net.onnx"))?;
// Process Discord attachment bytes
let image_bytes = download_from_discord(); // your function
let img = load_from_memory(&image_bytes)?;
// Process with custom options
let options = RemovalOptions::new()
.with_threshold(0.6)
.with_binary_mode(true);
let result = rembg.remove_background(img, options)?;
// Get raw image and mask for further processing
let (image, mask) = result.into_parts();
// Convert to bytes and send back to Discord
// See examples/discord_bot.rs for complete example
Ok(())
}
The library uses memory-mapped model loading - OS automatically manages memory:
See examples/ for more usage examples.
./download_model.sh
Available models:
# Basic usage
./rembg-rs -i input.jpg -o output.png -m models/u2net.onnx
# With custom threshold (0.0-1.0)
./rembg-rs -i input.jpg -o output.png -t 0.6
# Binary mode (clean cutout, no semi-transparency)
./rembg-rs -i input.jpg -o output.png -b
# Save mask alongside result
./rembg-rs -i input.jpg -o output.png -s
-i, --input <PATH> - Input image path-o, --output <PATH> - Output image path-m, --model <PATH> - Model file path (default: u2net.onnx)-t, --threshold <0.0-1.0> - Threshold (default: 0.5)
-b, --binary - Binary mode (no semi-transparency)-s, --save-mask - Save mask as separate file-q, --quality <1-100> - JPEG quality (default: 95)./download_model.sh
Add test images to test_input/ directory
Run the test script:
./test.sh
RembgMain struct for background removal operations.
Methods:
new(model_path: &str) -> Result<Self> - Create new instance with ONNX modelremove_background(image, options) -> Result<RemovalResult> - Process DynamicImageRemovalOptionsConfiguration for background removal.
Fields:
threshold: f32 - Alpha matting threshold (0.0-1.0)binary: bool - Binary mode (hard cutout vs soft edges)Methods:
default() - Create with default values (threshold: 0.5, binary: false)with_threshold(f32) - Set thresholdwith_binary_mode(bool) - Set binary modeRemovalResultResult of background removal.
Fields:
image: RgbaImage - Processed image with transparent backgroundmask: GrayImage - Mask used for removal (0-255)Methods:
image() - Get reference to RGBA imagemask() - Get reference to grayscale maskinto_parts() - Consume and return (image, mask)Note: File I/O operations are not part of the library core API.
Use the image crate to load/save images as needed.
The library uses memory-mapped model loading for optimal memory efficiency:
// Model is memory-mapped, not loaded into RAM
let mut rembg = Rembg::new("models/u2net.onnx")?;
How it works:
Usage:
use std::path::Path;
// Simple and efficient
let mut rembg = Rembg::new(Path::new("models/u2net.onnx"))?;
Benefits for Discord bots:
Memory usage:
lib.rs - Public API and main typeserror.rs - Error types with detailed documentationimage_processor.rs - Image loading, preprocessing, postprocessingmodel.rs - ONNX Runtime integrationcli.rs - CLI interface (optional, requires cli feature)main.rs - CLI entry point (requires cli feature)This library is designed to be used as a git dependency or published to crates.io.
default = [] - Library only, no CLIcli - Enables command-line interfaceMIT License - see LICENSE file