Site icon Coding is Love

How to remove background from an image using Javascript?

Removing background from an Image is something we need quite often. We’ll be searching for background removal tools. Here’s how to remove background programmatically using Javascript.

Introducing background-removal-js

Remove backgrounds from images directly in the browser environment with ease and no additional costs or privacy concerns.

Overview

@imgly/background-removal is a powerful npm package that allows developers to seamlessly remove the background from images directly in the browser. With its unique features and capabilities, this package offers an innovative and cost-effective solution for background removal tasks without compromising data privacy.

Key features

The key features of @imgly/background-removal are:

The Neural Network (ONNX model) and WASM files used by @imgly/background-removal 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 via npm or yarn. Use the following commands to install the package:

npm install @imgly/background-removal

Usage

import imglyRemoveBackground from "@imgly/background-removal"

let image_src: ImageData | ArrayBuffer | Uint8Array | Blob | URL | string = ...;

imglyRemoveBackground(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
  const url = URL.createObjectURL(blob);
})

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 behavior and give more control over the library.

type Config = {
  publicPath: string; // The public path used for model and wasm files
  debug: bool; // enable or disable useful console.log outputs
  proxyToWorker: bool; // Whether to proxy the calculations to a web worker. (Default true)
  model: 'small' | 'medium'; // The model to use. (Default "medium")
};

Download Size vs Quality

The onnx model is shipped in various sizes and needs.

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}`);
  }
}

Who is it for?

@imgly/background-removal is ideal for developers and projects that require efficient and cost-effective background removal directly in the browser. It caters to a wide range of use cases, including but not limited to:

Whether you are a professional developer or a hobbyist, @imgly/background-removal empowers you to deliver impressive applications and services with ease.

Demo of image with background removed

Live Demo

A live demo of the background removal tool in Javascript can be explored here

Exit mobile version