我有一个从 AWS S3 存储桶检索的图像,然后传递到 Dall E/OpenAI API。当我尝试时,我收到此错误响应:
message: "Invalid input image - format must be in ['RGBA', 'LA', 'L'], got RGB.",
我知道 RGB(我尝试上传的图像文件类型)包含一个 Alpha 通道,这本质上意味着图像上的透明区域。在将不良图像发送到 API 之前,是否可以/轻松地验证 NodeJS 中的图像类型以捕获不良图像?
我的 S3 得到一个像这样的 .png 文件:
const data = await s3Client.send(
new GetObjectCommand({
...bucketParams, // Bucket: <bucket name>
Key: `public/dalle/${inputParams.Key}`,
})
);
然后我通过 openai 库将其传递给 API:
const response = await openai.createImageEdit(
data.Body as unknown as File,
(maskImageBuffer as unknown as File) || data.Body,
prompt,
1,
"256x256"
);
你可以使用吉普
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.npmjs.com/package/jimp
https://www.tutorialspoint.com/how-to-change-the-opacity-of-an-image-in-node-jimp https://www.tutorialspoint.com/how-to-change-the-opacity-of-an-image-in-node-jimp
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)