Dall E API error: "Invalid input image - format must be in ['RGBA', 'LA', 'L'], got RGB."
Asked Answered
R

3

6

I have an image that I'm retrieving from an AWS S3 bucket and then passing to the Dall E/OpenAI API. When I attempt I get this error response:

message: "Invalid input image - format must be in ['RGBA', 'LA', 'L'], got RGB.",

I understand that RGB (the image file type I'm trying to upload) contains an alpha channel, which essentially means transparent areas on the image. Is it possible/easy to validate image types in NodeJS to catch bad images before I send them to the API?

My S3 gets a .png file like this:

    const data = await s3Client.send(
      new GetObjectCommand({
        ...bucketParams, // Bucket: <bucket name>
        Key: `public/dalle/${inputParams.Key}`,
      })
    );

And then I pass that to the API via the openai library:

    const response = await openai.createImageEdit(
      data.Body as unknown as File,
      (maskImageBuffer as unknown as File) || data.Body, 
      prompt,
      1,
      "256x256"
    );
Ramires answered 12/12, 2022 at 15:8 Comment(1)
I created a few validation functions as per this answer: https://mcmap.net/q/1917407/-how-to-validate-an-image-before-sending-to-dall-e-api-with-front-end-javascriptRamires
H
1

You can use Jimp

  let jImage = await Jimp.read(ImageBuffer);

  const w = jImage.bitmap.width; 
  const h = jImage.bitmap.height;

  if ((w / h) != 1) {
    throw new functions.https.
        HttpsError("invalid-argument",
            "Image must be a square. Current ratio = " + (w/h));
  }

  if (!jImage.hasAlpha()) { //Check if image has opacity
    jImage = jImage.opacity(1); //Add if it doesn't 
  }

  const jsize = (await jImage.getBufferAsync(Jimp.AUTO)).byteLength;

  if (jsize >= 4000000) { //Check size
    throw new functions.https.
        HttpsError("invalid-argument",
            "Image must be less than 4MG currenty image is " +
           jsize + " bytes with Alpha");
  }

  jImage.write("/tmp/fileName.png"); //Make PNG

https://www.npmjs.com/package/jimp

https://www.tutorialspoint.com/how-to-change-the-opacity-of-an-image-in-node-jimp

Hansen answered 9/3, 2023 at 15:14 Comment(0)
R
0

I created a few validation functions as per this answer: How to validate an image before sending to DALL E API with front-end javascript

Ramires answered 3/1, 2023 at 17:17 Comment(0)
S
0

On a Linux command line, you can use the convert command, which is a part of the ImageMagick suite of tools, to convert an image to an RGBA PNG file which I've tested OpenAI accepts:

Here's how to do it: If you haven't installed ImageMagick, you can usually install it using the package manager of your Linux distribution. For example, on Debian/Ubuntu:

sudo apt-get update
sudo apt-get install imagemagick

Or on CentOS:

sudo yum install ImageMagick

The use the convert command to perform the conversion:

convert input.jpg -alpha on -background none -flatten output.png

Replace input.jpg with the path to your source image, and output.png with the path where you want the RGBA PNG file to be saved.

Shore answered 20/8, 2023 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.