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
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.