OpenAI API Image Edit: Error 400 - PNG format and size limit issue

Hey everyone,

I’m stuck with an OpenAI Dall-e 2 image editing problem using the Node.js SDK. Here’s what’s going on:

const imageFile = fs.createReadStream(`./images/user_${userId}.png`)
const maskFile = fs.createReadStream(`./images/user_${userId}_mask.png`)

try {
  const result = await aiClient.images.edit({
    image: imageFile,
    mask: maskFile,
    prompt: userPrompt || 'Modify image based on prompt.',
  })
} catch (error) {
  console.error('Error:', error.message)
}

I keep getting a 400 error saying the image must be PNG and under 4MB. But I’m positive my files meet these requirements!

Am I missing something obvious? Maybe it’s a problem with how I’m reading the files?

Any ideas would be super helpful. Thanks!

I’ve run into this exact issue before, and it can be quite frustrating! From my experience, the problem might not be with your file format or size, but with how you’re passing the files to the API.

Instead of using fs.createReadStream(), try reading the files as buffers and then converting them to base64 strings. This approach has worked consistently for me:

const imageBuffer = fs.readFileSync(`./images/user_${userId}.png`);
const maskBuffer = fs.readFileSync(`./images/user_${userId}_mask.png`);

const result = await aiClient.images.edit({
  image: imageBuffer.toString('base64'),
  mask: maskBuffer.toString('base64'),
  prompt: userPrompt || 'Modify image based on prompt.',
});

This method ensures that the entire file content is loaded into memory and properly encoded before being sent to the API. It’s also worth double-checking that your PNG files are actually valid and not corrupted. Sometimes, files can appear to be the right format but have issues that the API detects.

If you’re still having trouble after trying this, it might be worth reaching out to OpenAI support directly. They’ve been quite helpful in my experience with API-related issues.

hey, have u tried using sharp library to resize and optimize ur images? it might help with the size issue. also, double-check ur file paths - sometimes that can cause weird errors. good luck!

I encountered a similar issue when working with the OpenAI API. One thing to consider is the Content-Type header. Make sure you’re setting it correctly to ‘multipart/form-data’ when sending the request. Also, verify that your PNG files are actually in the correct format and not just renamed JPEGs or other formats.

Another potential solution is to use the ‘form-data’ npm package to construct your request. It handles file uploads more reliably in my experience:

const FormData = require('form-data');
const form = new FormData();
form.append('image', fs.createReadStream(`./images/user_${userId}.png`));
form.append('mask', fs.createReadStream(`./images/user_${userId}_mask.png`));
form.append('prompt', userPrompt || 'Modify image based on prompt.');

const result = await aiClient.images.edit(form);

This approach has resolved similar 400 errors for me in the past. If the problem persists, consider using a tool like Postman to test the API directly and isolate whether the issue is with your code or the files themselves.