π 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.
-
AI-Powered Background Removal: Leverages state-of-the-art models via
rembg
:-
silueta
(43MB) - Default, optimized for human subjects -
u2netp
(4.7MB) - Lightweight model -
u2net
(176MB) - Original model with highest quality -
isnet-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.
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.
-
Install the Node.js package:
npm install ai-bg-remover # or yarn add ai-bg-remover
-
Install 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-learn
-
rembg
: 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):
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.
});
-
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-gradient
string (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 asoutputImage
with.svg
extension). 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)
-
-
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
}
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>
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 silueta
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
.
Contributions are welcome! Please feel free to submit pull requests or open issues.
MIT (Assuming you will add an MIT license file)