Can I use the URL of an image?
Option 1: Download the image locally
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
const path = require('path');
async function downloadImageAndCallAPI(imageUrl) {
try {
// Step 1: Download the image in the local variable `imageBytes`
const response = await axios.get(imageUrl, {
responseType: 'arraybuffer'
});
const imageBytes = response.data;
// Step 2: Call to the Remove Background API
const form = new FormData();
form.append('image_file', imageBytes, 'image.jpg');
// Step 3: Make an API call with the image bytes in multipart form
const uploadResponse = await axios.post("https://sdk.photoroom.com/v1/segment", form, {
headers: {
"x-api-key": "REPLACE_WITH_YOUR_API_KEY",
...form.getHeaders(),
},
responseType: 'arraybuffer'
});
// Step 3: Save the result image locally
const localPath = path.join(__dirname, 'photoroom-result.png');
fs.writeFileSync(localPath, uploadResponse.data);
} catch (error) {
console.error('An error occurred:', error.message);
}
}Option 2: Use the Image Editing API
Last updated
Was this helpful?