Node.js OpenAI API: Error when uploading image for DALL-E 2 editing

I'm having trouble with the OpenAI Node.js SDK while trying to edit an image using DALL-E 2. Here's what I'm doing:

1. Loading two PNG images (main image and mask) using fs.createReadStream
2. Calling the openai.images.edit function with these images and a prompt

But I'm getting a 400 error saying the image must be PNG and under 4 MB. I've double-checked, and I'm sure my images meet these requirements.

Here's a simplified version of my code:

```javascript
const mainImage = fs.createReadStream('user_image.png')
const maskImage = fs.createReadStream('mask_image.png')

try {
  const result = await aiClient.images.edit({
    image: mainImage,
    mask: maskImage,
    prompt: 'Transform the image based on this text'
  })
  console.log(result)
} catch (error) {
  console.error('Error:', error.message)
}

Any ideas why this might be happening? Could there be an issue with how I’m reading the files or passing them to the API? Thanks for any help!

hey spinninggalaxy, had similar iss. try using Buffer.from() instead of createReadStream() to load the files. like:

const mainImage = Buffer.from(fs.readFileSync(‘user_image.png’))
const maskImage = Buffer.from(fs.readFileSync(‘mask_image.png’))

this worked for me, hope it helps!

I’ve been using the OpenAI API for a while now, and I’ve found that the way you handle file uploads can make a big difference. In your case, I’d suggest trying to use the FormData API instead of streams. It’s been more reliable for me, especially with image uploads.

Here’s a quick example of how you might modify your code:

const FormData = require('form-data');
const fs = require('fs');

const form = new FormData();
form.append('image', fs.createReadStream('user_image.png'));
form.append('mask', fs.createReadStream('mask_image.png'));
form.append('prompt', 'Transform the image based on this text');

try {
  const result = await aiClient.images.edit(form);
  console.log(result);
} catch (error) {
  console.error('Error:', error.message);
}

This approach has worked well for me in similar situations. Give it a shot and see if it resolves your issue. If you’re still having trouble, it might be worth double-checking the file sizes and formats again, just to be sure.

yo spinninggalaxy, i ran into this too. check ur file sizes again - sometimes hidden metadata can push it over 4mb. also, try converting to jpg first then back to png. it sounds weird but it worked for me. good luck mate!

I encountered a similar issue when working with the OpenAI API for image editing. The problem might be related to how the API handles stream objects. Instead of using fs.createReadStream, try converting the images to base64 strings before sending them.

Here’s an example:

const fs = require('fs');
const path = require('path');

const mainImagePath = path.join(__dirname, 'user_image.png');
const maskImagePath = path.join(__dirname, 'mask_image.png');

const mainImage = fs.readFileSync(mainImagePath, { encoding: 'base64' });
const maskImage = fs.readFileSync(maskImagePath, { encoding: 'base64' });

Then use these base64 strings in your API call. This approach ensures the entire image data is available immediately, which might resolve the error you’re experiencing. Let me know if this works for you.