图像生成Beta Image generation Beta
学习如何使用我们的DALL·E模型生成或操作图像
图像生成概览 Introduction
Images API提供了三种与图像进行交互的方法:
- 基于文本提示从头创建图像
- 基于新的文本提示创建现有图像的编辑
- 创建现有图像的变体
本指南涵盖了使用这三个API端点(Completion)的基础知识,同时提供实用的代码示例。要查看它们的实际效果,请查看我们的DALL·E预览应用程序 (opens in a new tab)。
Images API目前处于beta版。在此期间,API和模型将根据您的反馈发展。
为确保所有用户都可以舒适地进行原型设计, 默认的速率限制是每分钟50张图片。
图像生成 Generations
图像生成image generations (opens in a new tab)端点(Completion)允许您根据文本提示创建原始图像。生成的图像可以有尺寸为256x256、512x512或1024x1024像素。较小的尺寸生成速度更快。您可以使用n (opens in a new tab)参数一次请求1-10个图像。
生成图像python选择库python node.js curl复制
response = openai.Image.create(
prompt="a white siamese cat",
n=1,
size="1024x1024"
)
image_url = response['data'][0]['url']
描述越详细,您或您的最终用户获得所期望的结果的可能性就越大。您可以在DALL·E预览应用程序 (opens in a new tab)中探索更多提示灵感的示例。下面是一个快速的示例:
Prompt | Generation |
---|---|
a white siamese cat | |
a close up, studio photographic portrait of a white siamese cat that looks curious, backlit ears |
每张图片都可以通过response_format (opens in a new tab)参数返回URL或Base64数据。URL将在一小时后过期。
图像编辑 Edits
图像编辑 (opens in a new tab)端点(Completion)允许您通过上传遮罩来编辑和扩展图像。遮罩的透明区域指示图像应该进行的编辑,提示应该描述完整的新图像,而不仅仅是已擦除的区域。该端点(Completion)可以实现类似于我们的DALL·E预览应用程序中的编辑器 (opens in a new tab)的体验。
response = openai.Image.create_edit(
image=open("sunlit_lounge.png", "rb"),
mask=open("mask.png", "rb"),
prompt="A sunlit indoor lounge area with a pool containing a flamingo",
n=1,
size="1024x1024"
)
image_url = response['data'][0]['url']
提示:带有一个含有火烈鸟的室内泳池的阳光明媚的休息室
上传的图像和遮罩必须都是小于4MB大小的正方形PNG图像,且它们必须具有相同的尺寸。生成输出时不使用遮罩的不透明区域,因此它们不一定需要像上面的示例那样匹配原始图像。
变体 Variations
图像变体 (opens in a new tab)端点(Completion)允许您生成给定图像的变体。
response = openai.Image.create_variation(
image=open("corgi_and_cat_paw.png", "rb"),
n=1,
size="1024x1024"
)
image_url = response['data'][0]['url']
与编辑端点(Completion)类似,输入图像必须是小于4MB大小的正方形PNG图像。
内容政策 Content moderation
基于我们的内容政策 (opens in a new tab),提示和图片会被过滤,如果提示或图片被标记,会返回一个错误。如果您对误判或相关问题有任何反馈,请通过我们的帮助中心 (opens in a new tab)联系我们。
语言特定提示
Node.js示例
上述指南中的Node.js示例使用fs
模块从磁盘读取图像数据。使用内存中的图像数据
在某些情况下,您可能希望在内存中存储图像数据。
这是一个使用存储在Node.js Buffer
对象中的图像数据的API调用示例:
// This is the Buffer object that contains your image data
const buffer = [your image data];
// Set a `name` that ends with .png so that the API knows it's a PNG image
buffer.name = "image.png";
const response = await openai.createImageVariation(
buffer,
1,
"1024x1024"
);
TypeScript示例
如果您正在使用TypeScript,您可能会遇到一些关于图像文件参数的怪异问题。
下面是一个通过显式转换参数来解决类型不匹配问题的示例:
// Cast the ReadStream to `any` to appease the TypeScript compiler
const response = await openai.createImageVariation(
fs.createReadStream("image.png") as any,
1,
"1024x1024"
);
下面是一个类似的内存图像数据示例:
// This is the Buffer object that contains your image data
const buffer: Buffer = [your image data];
// Cast the buffer to `any` so that we can set the `name` property
const file: any = buffer;
// Set a `name` that ends with .png so that the API knows it's a PNG image
file.name = "image.png";
const response = await openai.createImageVariation(
file,
1,
"1024x1024"
);
异常处理 Error handling
由于无效输入、速率限制或其他问题,API请求可能会返回错误。这些错误可以使用try...catch
语句处理,并且错误详细信息可以在error.response
或error.message
中找到:
try {
const response = await openai.createImageVariation(
fs.createReadStream("image.png"),
1,
"1024x1024"
);
console.log(response.data.data[0].url);
} catch (error) {
if (error.response) {
console.log(error.response.status);
console.log(error.response.data);
} else {
console.log(error.message);
}
}