gpt4 book ai didi

Flutter chat_gpt_sdk 错误 - 上传的图像必须是 png 且小于 4 mb

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

有人可以帮忙吗?我已经被困在这个问题上好几天了...我什至尝试将路径转换为 ​​base64 但没有任何效果...顺便说一句,该文件是 png,小于 1MB,并且是一个正方形。

这些建议也没有帮助:https://community.openai.com/t/using-image-url-in-images-edits-request/27247/3

void variation(File image, String name) async {
final request =
Variation(image: EditFile(image.path, name));
final response = await openAI.editor.variation(request);

print(response.data?.last?.url);
}

最佳答案

出现错误

  • 确保图像文件为 PNG 格式。如果将其转换为 PNG在将其传递给 API 之前需要。
  • 验证文件大小是否低于 4 MB。如果超过限制,压缩图像或调整图像大小以减小其大小。
  • 不转换为 Blob 或二进制,而是直接传递图像使用正确的文件路径或对象将 file 作为参数。

这对我有用

import 'dart:io';
import 'package:image/image.dart' as imageLib;
import 'package:openai/api.dart' as openAI;

void variation(File imageFile, String name) async {
// Verify image format and convert to PNG if necessary
if (!imageFile.path.toLowerCase().endsWith('.png')) {
File convertedImageFile = await convertToPNG(imageFile);
if (convertedImageFile == null) {
print('Error: Failed to convert image to PNG format.');
return;
}
imageFile = convertedImageFile;
}

// Verify file size and resize if necessary
int maxSizeInBytes = 4 * 1024 * 1024; // 4 MB
if (imageFile.lengthSync() > maxSizeInBytes) {
File resizedImageFile = await resizeImage(imageFile, maxSizeInBytes);
if (resizedImageFile == null) {
print('Error: Failed to resize image to reduce file size.');
return;
}
imageFile = resizedImageFile;
}

// Pass the image file as a parameter in the request
final request = openAI.Variation(
image: openAI.EditFile(imageFile.path, name),
);

final response = await openAI.editor.variation(request);

print(response.data?.last?.url);
}

// convert image file to PNG format
Future<File> convertToPNG(File imageFile) async {
try {
String newPath = imageFile.path.replaceAll(RegExp(r'\.\w+$'), '.png');
imageLib.Image image = imageLib.decodeImage(imageFile.readAsBytesSync());
File convertedImageFile = File(newPath)..writeAsBytesSync(imageLib.encodePng(image));
return convertedImageFile;
} catch (e) {
print('Error converting image to PNG: $e');
return null;
}
}

// Function to resize image file to reduce file size
Future<File> resizeImage(File imageFile, int maxSizeInBytes) async {
try {
double quality = 0.9; // Adjust the quality of the resized image as needed
imageLib.Image image = imageLib.decodeImage(imageFile.readAsBytesSync());
int currentSizeInBytes = imageFile.lengthSync();
while (currentSizeInBytes > maxSizeInBytes) {
image = imageLib.copyResize(image, width: (image.width * 0.9).round());
List<int> resizedImageBytes = imageLib.encodeJpg(image, quality: quality * 100).toList();
currentSizeInBytes = resizedImageBytes.length;
imageFile = File(imageFile.path)..writeAsBytesSync(resizedImageBytes);
}
return imageFile;
} catch (e) {
print('Error resizing image: $e');
return null;
}
}

关于Flutter chat_gpt_sdk 错误 - 上传的图像必须是 png 且小于 4 mb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76634093/

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