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"
);
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