Image generation Beta
Learn how to generate or manipulate images with our DALL·E models 了解如何使用我们的 DALL·E 模型生成或处理图像
Introduction
The Images API provides three methods for interacting with images: 图片 API 提供了三种与图片交互的方法:
- Creating images from scratch based on a text prompt 根据文本提示从头开始创建图像
- Creating edits of an existing image based on a new text prompt 根据新文本提示创建现有图像的编辑
- Creating variations of an existing image 创建现有图像的变体
This guide covers the basics of using these three API endpoints with useful code samples. To see them in action, check out our DALL·E preview app. 本指南涵盖了使用这三个 API 端点的基础知识以及有用的代码示例。要查看它们的实际效果,请查看我们的 DALL·E 预览应用程序。
The Images API is in beta. During this time the API and models will evolve based on your feedback. To ensure all users can prototype comfortably, the default rate limit is 50 images per minute. If you would like to increase your rate limit, please review this help center article. We will increase the default rate limit as we learn more about usage and capacity requirements. 图片 API 处于测试阶段。在此期间,API 和模型将根据您的反馈进行改进。为确保所有用户都能轻松制作原型,默认速率限制为每分钟 50 张图像。如果您想提高速率限制,请查看这篇帮助中心文章。随着我们对使用和容量要求的更多了解,我们将提高默认速率限制。
Usage
Generations
The image generations endpoint allows you to create an original image given a text prompt. Generated images can have a size of 256x256, 512x512, or 1024x1024 pixels. Smaller sizes are faster to generate. You can request 1-10 images at a time using the n parameter. 图像生成端点允许您在给定文本提示的情况下创建原始图像。生成的图像的大小可以为 256x256、512x512 或 1024x1024 像素。较小的尺寸生成速度更快。您可以使用 n 参数一次请求 1-10 张图像。
Generate an image 生成图像
python
response = openai.Image.create(
prompt="a white siamese cat",
n=1,
size="1024x1024"
)
image_url = response['data'][0]['url']
The more detailed the description, the more likely you are to get the result that you or your end user want. You can explore the examples in the DALL·E preview app for more prompting inspiration. Here's a quick example: 描述越详细,您就越有可能获得您或您的最终用户想要的结果。您可以探索 DALL·E 预览应用程序中的示例以获得更多提示灵感。这是一个简单的例子:
PROMPT | GENERATION |
---|---|
a white siamese cat 一只白色的暹罗猫 | |
a close up, studio photographic portrait of a white siamese cat that looks curious, backlit ears 一只白色暹罗猫的特写工作室摄影肖像,它看起来好奇,背光的耳朵 |
Each image can be returned as either a URL or Base64 data, using the response_format parameter. URLs will expire after an hour. 使用 response_format 参数,每个图像都可以作为 URL 或 Base64 数据返回。 URL 将在一小时后过期。
Edits
The image edits endpoint allows you to edit and extend an image by uploading a mask. The transparent areas of the mask indicate where the image should be edited, and the prompt should describe the full new image, not just the erased area. This endpoint can enable experiences like the editor in our DALL·E preview app. 图像编辑端点允许您通过上传蒙版来编辑和扩展图像。遮罩的透明区域指示应编辑图像的位置,提示应描述完整的新图像,而不仅仅是擦除区域。此端点可以启用类似我们 DALL·E 预览应用程序中的编辑器的体验。
Edit an image 编辑图像
python
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']
IMAGE | MASK | OUTPUT |
---|---|---|
Prompt: a sunlit indoor lounge area with a pool containing a flamingo 提示:一个阳光充足的室内休息区,带有一个包含火烈鸟的游泳池
The uploaded image and mask must both be square PNG images less than 4MB in size, and also must have the same dimensions as each other. The non-transparent areas of the mask are not used when generating the output, so they don’t necessarily need to match the original image like the example above. 上传的图片和遮罩必须是小于 4MB 的正方形 PNG 图片,并且必须具有相同的尺寸。生成输出时不使用遮罩的非透明区域,因此它们不一定需要像上面的示例那样与原始图像匹配。
Variations
The image variations endpoint allows you to generate a variation of a given image. 图像变体端点允许您生成给定图像的变体。
Generate an image variation 生成图像变体
python
response = openai.Image.create_variation(
image=open("corgi_and_cat_paw.png", "rb"),
n=1,
size="1024x1024"
)
image_url = response['data'][0]['url']
IMAGE | OUTPUT |
---|---|
Similar to the edits endpoint, the input image must be a square PNG image less than 4MB in size. 与编辑端点类似,输入图像必须是大小小于 4MB 的方形 PNG 图像。
Prompts and images are filtered based on our content policy, returning an error when a prompt or image is flagged. If you have any feedback on false positives or related issues, please contact us through our help center. 提示和图像根据我们的内容政策进行过滤,当提示或图像被标记时返回错误。如果您对误报或相关问题有任何反馈,请通过我们的帮助中心联系我们。
NODE.JSPYTHON
Using in-memory image data 使用内存图像数据
The Node.js examples in the guide above use the fs
module to read image data from disk. In some cases, you may have your image data in memory instead. Here's an example API call that uses image data stored in a Node.js Buffer
object: 上面指南中的 Node.js 示例使用 fs
模块从磁盘读取图像数据。在某些情况下,您可能会将图像数据保存在内存中。下面是一个 API 调用示例,它使用存储在 Node.js Buffer
对象中的图像数据:
// 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"
);
Working with TypeScript 使用TypeScript
If you're using TypeScript, you may encounter some quirks with image file arguments. Here's an example of working around the type mismatch by explicitly casting the argument: 如果您使用的是 TypeScript,您可能会遇到一些图像文件参数的问题。下面是通过显式转换参数来解决类型不匹配的示例:
// Cast the ReadStream to `any` to appease the TypeScript compiler
const response = await openai.createImageVariation(
fs.createReadStream("image.png") as any,
1,
"1024x1024"
);
And here's a similar example for in-memory image data: 这是内存中图像数据的类似示例:
// 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 requests can potentially return errors due to invalid inputs, rate limits, or other issues. These errors can be handled with a try...catch
statement, and the error details can be found in either error.response
or error.message
: 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);
}
}
PYTHON
Using in-memory image data 使用内存图像数据
The Python examples in the guide above use the open
function to read image data from disk. In some cases, you may have your image data in memory instead. Here's an example API call that uses image data stored in a BytesIO
object: 上面指南中的 Python 示例使用 open
函数从磁盘读取图像数据。在某些情况下,您可能会将图像数据保存在内存中。下面是一个 API 调用示例,它使用存储在 BytesIO
对象中的图像数据:
from io import BytesIO
# This is the BytesIO object that contains your image data
byte_stream: BytesIO = [your image data]
byte_array = byte_stream.getvalue()
response = openai.Image.create_variation(
image=byte_array,
n=1,
size="1024x1024"
)
Operating on image data 图像数据操作
It may be useful to perform operations on images before passing them to the API. Here's an example that uses PIL
to resize an image: 在将图像传递给 API 之前对图像执行操作可能很有用。这是一个使用 PIL
调整图像大小的示例:
from io import BytesIO
from PIL import Image
# Read the image file from disk and resize it
image = Image.open("image.png")
width, height = 256, 256
image = image.resize((width, height))
# Convert the image to a BytesIO object
byte_stream = BytesIO()
image.save(byte_stream, format='PNG')
byte_array = byte_stream.getvalue()
response = openai.Image.create_variation(
image=byte_array,
n=1,
size="1024x1024"
)
Error handling
API requests can potentially return errors due to invalid inputs, rate limits, or other issues. These errors can be handled with a try...except
statement, and the error details can be found in e.error
: API 请求可能会由于无效输入、速率限制或其他问题而返回错误。这些错误可以用 try...except
语句处理,错误详细信息可以在 e.error
中找到:
try:
openai.Image.create_variation(
open("image.png", "rb"),
n=1,
size="1024x1024"
)
print(response['data'][0]['url'])
except openai.error.OpenAIError as e:
print(e.http_status)
print(e.error)