@harshit_01/ai-bg-remover v1.0.2
AI Background Remover
š A high-performance AI-powered background removal library for Node.js, utilizing the powerful rembg Python library.
Easily remove backgrounds from images, apply custom backgrounds (solid colors or gradients), enhance image quality, and even convert the result to a high-fidelity SVG vector format.
Features
- AI-Powered Background Removal: Leverages state-of-the-art models via
rembg:silueta(43MB) - Default, optimized for human subjectsu2netp(4.7MB) - Lightweight modelu2net(176MB) - Original model with highest qualityisnet-general-use(44MB) - Medium size with good performance
- Custom Backgrounds: Replace the removed background with solid colors (HEX) or linear gradients.
- Image Enhancement: Basic image sharpening and contrast adjustments.
- Vector Conversion: Convert the background-removed image into a scalable SVG format.
- Simple Node.js API: Easy integration into your Node.js projects.
- Command-Line Interface: Includes a Python script for direct command-line usage.
Prerequisites
This library acts as a Node.js wrapper around a Python script. Therefore, you need Python installed on your system, along with the necessary Python packages.
- Python: Ensure Python 3.7+ is installed and accessible in your system's PATH.
- Pip: Python's package installer.
Installation
Install the Node.js package:
npm install ai-bg-remover # or yarn add ai-bg-removerInstall required Python packages:
pip install rembg[gpu] # Or rembg[cpu] if you don't have a compatible GPU pip install opencv-python numpy Pillow scikit-image scikit-learnrembg: The core background removal library.opencv-python: For image processing tasks.numpy: Required by OpenCV and other libraries.Pillow: Image processing library.scikit-image,scikit-learn: Used for advanced image segmentation and vector conversion.
(Optional) Install SVG Optimization Tools: For optimizing the generated SVG files:
- SVGO (Recommended):
npm install -g svgo - Scour (Python):
pip install scour
- SVGO (Recommended):
Usage (Node.js)
const path = require('path');
const { removeBackground } = require('ai-bg-remover');
const inputFile = path.join(__dirname, 'input.jpg');
const outputFile = path.join(__dirname, 'output.png');
const options = {
// background: '#FFFFFF', // Solid white background
background: 'linear-gradient(to right, #ff7e5f, #feb47b)', // Gradient background
enhance: true, // Enhance image quality
vector: true, // Convert output to SVG
// vectorQuality: 'medium' // SVG quality: 'low', 'medium', 'high' (default)
model: 'silueta' // Model to use: 'silueta' (default), 'u2netp', 'u2net', 'isnet-general-use'
};
removeBackground(inputFile, outputFile, options)
.then(result => {
console.log('Success:', result);
// Sample Response:
// {
// success: true,
// outputPath: '/path/to/output.png',
// message: 'Background removed successfully'
// }
})
.catch(err => {
console.error(`ā Error: ${err}`);
// Sample Error Response:
// ā Error: The input file does not exist: /path/to/input.jpg
// or
// ā Error: Unsupported file format. Please use JPG or PNG.
// or
// ā Error: File size exceeds the 5MB limit.
});API
removeBackground(inputImage, outputImage, [options])
inputImage(String): Path to the input image file (JPG, JPEG, PNG).outputImage(String): Path to save the output PNG image.options(Object, Optional): Configuration options:background(String): Background to apply.- Solid Color: HEX code (e.g.,
"#FF0000","#fff"). - Gradient: CSS-like
linear-gradientstring (e.g.,"linear-gradient(to right, #ff7e5f, #feb47b)"). Requires at least two HEX colors.
- Solid Color: HEX code (e.g.,
enhance(Boolean): Iftrue, applies basic image enhancement. Defaults tofalse.vector(Boolean): Iftrue, converts theoutputImage(PNG) to an SVG file (saved asoutputImagewith.svgextension). Defaults tofalse.vectorQuality(String): Quality preset for SVG conversion ('low','medium','high'). Defaults to'high'.model(String): Model to use for background removal. Options:'silueta'(default) - Optimized for human subjects (43MB)'u2netp'- Lightweight model (4.7MB)'u2net'- Original model with highest quality (176MB)'isnet-general-use'- Medium size with good performance (44MB)
Response Format
The function returns a Promise that resolves to an object with the following structure:
{
success: true, // Boolean indicating if the operation was successful
outputPath: string, // Path to the processed image
message: string // Success message
}Error Handling
The function will reject the Promise with an error message in the following cases:
- File does not exist:
ā Error: The input file does not exist: <path> - Unsupported format:
ā Error: Unsupported file format. Please use JPG or PNG. - File too large:
ā Error: File size exceeds the 5MB limit. - Processing error:
ā Error: <error message from Python script>
Command-Line Usage (Python)
You can also use the underlying Python script directly:
python src/remove_bg.py <input_path> <output_path> [options]Options:
--background "<value>": Set background color or gradient.--enhance: Enable image enhancement.--vector: Enable SVG conversion.--model <name>: Model to use (default: silueta)- Choices: silueta, u2netp, u2net, isnet-general-use
Example:
python src/remove_bg.py images/input.jpg results/output.png --enhance --vector --model siluetaHow it Works
This library executes the src/remove_bg.py Python script using Node.js's child_process. The Python script handles the core image processing tasks using rembg and opencv-python.
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues.
License
MIT (Assuming you will add an MIT license file)