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"
    );
Helen
  • 87,344
  • 17
  • 243
  • 314
JimmyTheCode
  • 3,783
  • 7
  • 29
  • 71

3 Answers3

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

lorem ipsum
  • 21,175
  • 5
  • 24
  • 48
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

JimmyTheCode
  • 3,783
  • 7
  • 29
  • 71
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.

Timothy Bushell
  • 170
  • 1
  • 7