I am trying to use the Remove.bg API in a plain JS file, with the goal of passing image URLS as parameters and then outputting the resulting image to an IMG tag within the HTML body.
Unfortunately, the project does not provide documentation for such usage, as the results.data is intended to be used by FileSystem's writeFileSync, which I cannot use as I am running a static website.
Is there per chance, any way to parse the results as described and perhaps set an IMG tag's src to the resulting, background-removed image?
Below is an example code that so far DOES return a response. Thank you in advance for your time!
function removebackground(){
let formData = {
"image_file_b64": "",
"image_url": "https://meta.hapeprime.com/7386.png",
"size": "preview",
"type": "auto",
"type_level": "1",
"format": "auto",
"roi": "0% 0% 100% 100%",
"crop": false,
"crop_margin": "0",
"scale": "original",
"position": "original",
"channels": "rgba",
"add_shadow": false,
"semitransparency": true,
"bg_color": "",
"bg_image_url": ""
};
axios({
method: 'post',
url: 'https://api.remove.bg/v1.0/removebg',
data: formData,
responseType: 'arraybuffer',
headers: {
'X-Api-Key': 'API_KEY',
},
encoding: null
})
.then((response) => {
if(response.status != 200) return console.error('Error:', response.status, response.statusText);
console.log(response);
})
.catch((error) => {
return console.error('Request failed:', error);
});
}