Remove backgrounds from images in NodeJs environment with ease and no additional costs or privacy concerns.
Explore an interactive demo.
News
-
September 12th, 2023
: We released the code of Background Removal NodeJS.
Overview
@imgly/background-removal-node-node
is a powerful npm package that allows developers to seamlessly remove the background from images in NodeJs. With its unique features and capabilities, this package offers an innovative and cost-effective solution for background removal tasks without compromising data privacy.
The key features of @imgly/background-removal-node-node
are:
-
Seamless Integration with IMG.LY’s CE.SDK:
@imgly/background-removal-node
provides seamless integration with IMG.LY’s CE.SDK, allowing developers to easily incorporate powerful NodeJS image matting and background removal capabilities into their projects.
The Neural Network (ONNX model) are hosted on UNPKG, making it readily available for download to all users of the library. See the section Custom Asset Serving if you want to host data on your own servers.
Installation
You can install @imgly/background-removal-node
via npm or yarn. Use the following commands to install the package:
NPM
npm install @imgly/background-removal-node
Usage
import {removeBackground} from "@imgly/background-removal-node"; // const {removeBackground} = require("@imgly/background-removal-node"); let image_src: ImageData | ArrayBuffer | Uint8Array | Blob | URL | string = ...; removeBackground(image_src).then((blob: Blob) => { // The result is a blob encoded as PNG. It can be converted to an URL to be used as HTMLImage.src })
Note: On the first run the wasm and onnx model files are fetched. This might, depending on the bandwidth, take time. Therefore, the first run takes proportionally longer than each consecutive run. Also, all files are cached by the browser and an additional model cache.
Advanced Configuration
The library does not need any configuration to get started. However, there are optional parameters that influence the behaviour and give more control over the library.
type Config = { publicPath: string; // The public path used for model a debug: bool; // enable or disable useful console.log outputs model: 'small' | 'medium'; // The model to use. (Default "medium") };
Download Size vs Quality
The onnx model is shipped in various sizes and needs.
- small (~40 MB) is the smallest model and is in most cases working fine but sometimes shows some artifacts. It’s a quantized model.
- medium (~80MB) is the default model.
Download Progress Monitoring
On the first run, the necessary data will be fetched and stored in the browser cache. Since the download might take some time, you have the option to tap into the download progress.
let config: Config = { progress: (key, current, total) => { console.log(`Downloading ${key}: ${current} of ${total}`); } };
Custom Asset Serving
Currently, the wasm and onnx neural networks are served via unpkg. For production use, we advise you to host them yourself. Therefore, copy all .wasm and .onnx files to your public path