Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • PDF Invoice Generator
Menu

Node.js Tutorial to Remove Background From Image Using Rembg & Sharp Library in Command Line

Posted on May 19, 2023

 

 

Welcome folks today in this blog post we will be using the sharp and rembg library to remove background from image in command line using node.js. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make a new node.js project using the below command as shown below

 

 

npm init -y

 

 

npm i sharp

 

 

npm i rembg-node

 

 

And after that you need to make a new index.js file and copy paste the following code

 

 

index.js

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const { Rembg } = require("rembg-node");
const sharp = require("sharp");
 
(async () => {
const input = sharp("1.jpg");
 
// optional arguments
const rembg = new Rembg({
logging: true,
});
 
const output = await rembg.remove(input);
 
await output.webp().toFile("test-output.webp");
 
// optionally you can use .trim() too!
await output.trim().webp().toFile("test-output-trimmed.webp");
})();

 

 

As you can see we have imported the sharp and rembg-node libraries at the top and then we are removing the background from the images and then we are saving it as the webp image.

 

Recent Posts

  • Node.js Express Project to Remove Background of Images Using Rembg & Formidable Library in Browser
  • Node.js Tutorial to Remove Background From Image Using Rembg & Sharp Library in Command Line
  • Python 3 Flask Project to Remove Background of Multiple Images Using Rembg Library in Browser
  • Python 3 Rembg Library Script to Bulk Process Multiple Images and Remove Background in Command Line
  • Python 3 Rembg Library Script to Remove Background From Image in Command Line
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme