gpt4 book ai didi

node.js - Dall E API 错误 : "Invalid input image - format must be in [' RGBA', 'LA' , 'L' ],得到 RGB。”

转载 作者:行者123 更新时间:2023-12-02 22:44:26 25 4
gpt4 key购买 nike

我有一张图像,我正在从 AWS S3 存储桶中检索该图像,然后将其传递给 Dall E/OpenAI API。当我尝试时收到此错误响应:

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

我知道 RGB(我要上传的图像文件类型)包含一个 alpha channel ,这实际上意味着图像上的透明区域。是否可以/容易地验证 NodeJS 中的图像类型以在我将它们发送到 API 之前捕获不良图像?

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

最佳答案

你可以使用 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

关于node.js - Dall E API 错误 : "Invalid input image - format must be in [' RGBA', 'LA' , 'L' ],得到 RGB。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74773173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com