I have been trying to write a program that removes an image's original background and allows the user to add different colour backgrounds. I'm able to accomplish the first part by using the removebg API. But I'm unable to add background to the image. All I can find online is how to add a background to a div element or a form element. Please help. This is the code until now:
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
const path = require('path');
const inputPath = 'ctive.png';
const formData = new FormData();
formData.append('size', 'auto');
formData.append('image_file', fs.createReadStream(inputPath), path.basename(inputPath));
axios({
method: 'post',
url: 'https://api.remove.bg/v1.0/removebg',
data: formData,
responseType: 'arraybuffer',
headers: {
...formData.getHeaders(),
'X-Api-Key': 'YUCKwSujDYGm2z1FpVHgxxo8',
},
encoding: null
})
.then((response) => {
if (response.status != 200) return console.error('Error:', response.status, response.statusText);
fs.writeFileSync("no-bg-pingu.png", response.data);
})
.catch((error) => {
return console.error('Request failed:', error);
});