Node.js Express Project to Remove Background From Image File or URL Using remove.bg API Module Library in Javascript Full Tutorial For Beginners

Node.js Express Project to Remove Background From Image File or URL Using remove.bg API Module Library in Javascript Full Tutorial For Beginners

 

 

 

Welcome folks today in this blog post we will be removing background from image in node.js and express using remove.bg api. All the source code of the application will be shown below.

 

 

 

Live Demo

 

 

Remove Background From Image

 

Remove Background From URL Image

 

 

Screenshot

 

 

 

 

 

 

 

Get Started

 

 

 

In order to get started we need to install the remove.bg library by issuing the npm command

 

npm i remove.bg

 

 

 

 

 

After installing this just make a index.js file and copy paste the following code

 

index.js

 

First of all we will be removing background from image file using remove.bg api key

 

First of all go to remove.bg website and create an account and get an api key

 

 

 

 

 

 

 

After you get your api key you can use the below example to remove background from image file

READ  Build a Coronavirus COVID-19 Country Live Status (Active + Recovered + Deaths) Cases Tracker in Browser Using Vanilla Javascript Full Project For Beginners

 

 

const {removeBackgroundFromImageUrl,removeBackgroundFromImageFile} = require("remove.bg")
 
const localFile = "./local/file/name.jpg";
const outputFile = `${__dirname}/out/img-removed-from-file.png`;
 
removeBackgroundFromImageFile({
  path: localFile,
  apiKey: "YOUR-API-KEY",
  size: "regular",
  type: "auto",
  scale: "50%",
  outputFile 
}).then((result) => {
 console.log(`File saved to ${outputFile}`);
}).catch((errors) => {
 console.log(JSON.stringify(errors));
});

 

 

Now if you execute this node.js app by executing the following command

 

node index.js

 

 

 

 

As you can see this is the original image with background now if you execute this by this library the output will be shown below with no background

 

 

 

You can also pass a Image url and then remove background from it. All the source code of that example is shown below

 

 

const {removeBackgroundFromImageUrl,removeBackgroundFromImageFile} = require("remove.bg")
 
const url = "https://domain.tld/path/file.jpg";
const outputFile = `${__dirname}/out/img-removed-from-file.png`;
 
removeBackgroundFromImageUrl({
  url,
  apiKey: "YOUR-API-KEY",
  size: "regular",
  type: "person",
  outputFile 
}).then((result) => {
 console.log(`File saved to ${outputFile}`);
  const base64img = result.base64img;
}).catch((errors) => {
 console.log(JSON.stringify(errors));
});

 

Leave a Reply