npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

node-rembg

v1.0.2

Published

A Node.js wrapper for the rembg Python tool to remove image backgrounds.

Downloads

26

Readme

Node.js Rembg Wrapper

This package provides a simple Node.js wrapper for the rembg Python tool. It allows you to remove the background from images by calling the rembg command-line interface from your Node.js code.

Prerequisites

Before using this package, you must have the following installed on your system:

  1. Python: Ensure Python is installed and accessible from your command line.

  2. Rembg: You need to install the rembg library using pip.

    pip install rembg

    Also, make sure the rembg command is available in your system's PATH. You can verify this by running rembg --version in your terminal.

Installation

You can install the package via npm:

npm install node-rembg

(Note: This assumes the package is published to npm with the name node-rembg. For local use, you can just include it in your project).

Usage

The package exports a single function, removeBackground, which takes an input path, an output path, and an optional options object as arguments. It returns a Promise that resolves when the operation is complete.

Here's a basic example:

// example.js
const { removeBackground } = require('./index'); // Use require('node-rembg') if installed from npm
const path = require('path');

const inputImagePath = path.join(__dirname, 'my-image.jpg');
const outputImagePath = path.join(__dirname, 'my-image-no-bg.png');

// By default, the process times out after 10 seconds.
// You can override this by providing a different timeout. For example, 30 seconds:
const options = {
  timeout: 30000,
};

console.log('Starting background removal...');

// Call with custom options
removeBackground(inputImagePath, outputImagePath, options)
  .then((outputPath) => {
    console.log(`Successfully removed background. Output saved to: ${outputPath}`);
  })
  .catch((error) => {
    console.error('An error occurred during background removal:');
    console.error(error);
  });

// ... or call without options to use the default 10-second timeout
removeBackground(inputImagePath, outputImagePath)
  .then((outputPath) => {
    console.log(`Successfully removed background. Output saved to: ${outputPath}`);
  })
  .catch((error) => {
    console.error('An error occurred during background removal:');
    console.error(error);
  });

Removing Background from a URL

You can also process an image directly from a URL.

const { removeBackgroundFromUrl } = require('./index');
const path = require('path');

const imageUrl = 'https://www.example.com/image.jpg';
const outputPath = path.join(__dirname, 'url-image-no-bg.png');

removeBackgroundFromUrl(imageUrl, outputPath)
  .then((savedPath) => {
    console.log(`Background removed and image saved to: ${savedPath}`);
  })
  .catch((error) => {
    console.error('Failed to remove background from URL:', error);
  });

Parameters

removeBackground(inputPath, outputPath, [options])

  • inputPath (string): The path to the input image.
  • outputPath (string): The path to save the output image.
  • options (object, optional):
    • timeout (number): The maximum time in milliseconds the process is allowed to run. Defaults to 10000 (10 seconds).

removeBackgroundFromUrl(imageUrl, outputPath, [options])

  • imageUrl (string): The URL of the input image.
  • outputPath (string): The path to save the output image.
  • options (object, optional):
    • timeout (number): The maximum time in milliseconds for the process. Defaults to 10000 (10 seconds).

How to Run the Example

  1. Place an image file named my-image.jpg in the same directory as example.js.

  2. Run the script from your terminal:

    node example.js
  3. A new file, my-image-no-bg.png, with the background removed, will be created in the same directory.

Error Handling

The promise will be rejected if the rembg process fails. This could be due to several reasons:

  • rembg is not installed or not in the system's PATH.
  • The input file does not exist or is not a valid image.
  • Other processing errors from rembg itself.

The error object will contain details about the failure.

  • The rembg process can be terminated if it exceeds the timeout specified in the options.
  • The input file does not exist or is not a valid image.
  • Other processing errors from rembg itself. # node-rembg