Skip to content

A State-of-the-art Node.js npm library that can be used in to remove background 100% automatically using AI

License

Notifications You must be signed in to change notification settings

Remove-Background-ai/rembg.js

Repository files navigation

www.rembg.com API wrapper for Node.js

A simple, FREE AI background removal tool for Node.js. Currently, this is THE ONLY FREE library available for personal usage from API. Check out our website at www.rembg.com for more details.

Requirements

Get your FREE API Key from the https://www.rembg.com/api-usage PS: you can still use the API Keys without key but it is very limited

Installation

npm i --save @remove-background-ai/rembg.js

Library

inputImage can be one of these: string | Buffer | { base64: string };

// script.mjs file

import  { rembg } from '@remove-background-ai/rembg.js';
import dotenv from 'dotenv';
// Load environment variables from .env file
dotenv.config();

// API_KEY will be loaded from the .env file
const API_KEY = process.env.API_KEY;

// log upload and download progress
const onDownloadProgress = console.log;
const onUploadProgress = console.log;

rembg({
    apiKey: API_KEY,
    inputImage: './input.png',
    onDownloadProgress,
    onUploadProgress
}).then(({ outputImagePath, cleanup }) => {
    console.log(`✅🎉 background removed and saved under path=${outputImagePath}`);
    // if called, it will cleanup (remove from disk) your removed background image
    // cleanup();
});

Base64 return type example

if you wish to return a Base64 instead of temporary URL you can use retrunBase64 parameter:

import { rembg } from '../dist/index.js';
import dotenv from 'dotenv';
// Load environment variables from .env file
dotenv.config();

// API_KEY will be loaded from the .env file
const API_KEY = process.env.API_KEY;
// log upload and download progress
const onDownloadProgress = console.log;
const onUploadProgress = console.log;

rembg({
    apiKey: API_KEY,
    inputImage: './input.png',
    onDownloadProgress,
    onUploadProgress,
    returnBase64: true
}).then(({ base64Image }) => {
    console.log(`✅🎉 background removed ${base64Image}`);
});

Input Image as Base64 or Buffer

Since version 1.1.8, the rembg function can accept an input image as a Buffer or Base64 object. Below is a quick demonstration:

import { rembg } from '../dist/index.js';
import dotenv from 'dotenv';
// Load environment variables from .env file
dotenv.config();

const base64Input = 'data:image/png;base64,/9j/4AAQSkZJRgABAQEASABIAAD/.....etc'
// API_KEY will be loaded from the .env file
const API_KEY = process.env.API_KEY;
// log upload and download progress
const onDownloadProgress = console.log;
const onUploadProgress = console.log;

rembg({
    apiKey: API_KEY,
    inputImage: {base64: base64Input}, // or simply an imageBuffer
    onDownloadProgress,
    onUploadProgress,
    returnBase64: true
}).then(({ base64Image }) => {
    console.log(`✅🎉 background removed ${base64Image}`);
});

Usage of returnMask flag

The library provides an option to return a mask of the image instead of the processed image. This is controlled by the returnMask parameter. When set to true, the function returns a mask. By default (if omitted), this parameter is set to false.

Code Snippet

rembg({
    apiKey: API_KEY,
    inputImage: './input.jpg',
    onDownloadProgress,
    onUploadProgress,
    returnMask: true, // <----- Set to true to get the mask of the image
    returnBase64: true // Set to true to receive the result as a Base64 string
}).then(({ base64Image }) => {
    console.log(`✅🎉 Mask retrieved: ${base64Image}`);
});

Below is the generated mask image using the rembg function with the mask option enabled:

Generated Mask

This image demonstrates the result of the mask generation process. The mask typically highlights the main subject of the image with the background removed or made transparent.

This is very useful to work with Stable Diffusion for perfect area of inpainting, for example.

About

A State-of-the-art Node.js npm library that can be used in to remove background 100% automatically using AI

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published