I am trying to use the image-to-image API from stabilityAI. I need to send an image as binary string in my express server to the stability AI API, when I send the POST request to the API I get this error:
return
invalid mime type for init_image: application/octet-stream is not image/jpeg, image/png, image/gif, or image/webp.
The documentation says I need to send it as string binary I am sending the image from the frontend as a base 64 and receive it in this function.
app.post('/transform', async(req, res) => {
const formData = new FormData();
let data = req.body.image;
// Convert Base64 to binary
const imagePath = 'src/assets/images/image2.png';
const imageBuffer = Buffer.from(data, 'base64');
fs.writeFileSync(imagePath, imageBuffer);
formData.append('init_image', imageBuffer, {filename: 'image.jpg', contentType: 'image/png'})
formData.append('init_image_mode', 'IMAGE_STRENGTH')
formData.append('image_strength', '.40')
formData.append('text_prompts[0][text]', 'Galactic dog wearing a cape')
formData.append('text_prompts[0][weight]', '0.5'); // Sample weight
formData.append('cfg_scale', '7')
formData.append('clip_guidance_preset', 'FAST_BLUE')
formData.append('samples', '1')
formData.append('steps', '30')
try {
const response = await axios.post(url, formData, {
headers: {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json', // Update the Accept header
'Authorization': `Bearer ${apiKey}`,
},
});
const responseData = response.data;
console.log(responseData);
res.send(responseData);
} catch (error) {
console.error(error);
res.sendStatus(500);
}
})
I tried sending it as base 64 and converting it to binary but it doesn't seem to work. Any help would be appreciated.
What worked for me was to add
const FormData = require('form-data');at the top, whereas I didn't have anything before.I don't exactly know why this fixed it, but I guess there are two different
FormDatamodules kicking around, and the "built-in" one behaves differently in a way that the stability.ai API doesn't like.