REF: https://platform.openai.com/docs/api-reference/images
给定一个提示和/或输入图像,该模型将生成一张新图像。
相关指引: 图片生成
Image generation
学习如何使用我们的 DALL·E 模型生成或操作图像
介绍
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目前处于测试阶段。在此期间,API和模型将根据您的反馈进行改进。为了确保所有用户都能轻松地进行原型设计, 默认速率限制为每分钟50张图片。如果您想增加速率限制,请查看此帮助中心文章。随着我们了解更多的使用和容量要求,我们将增加默认速率限制。
Usage
Generations
图像生成端点允许您根据给定的文本提示创建原始图像。生成的图像可以是256x256,512x512或1024x1024像素的大小。较小的尺寸生成速度更快。您可以使用“n”参数一次请求1-10张图片。
curl https://api.openai.com/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"prompt": "a white siamese cat",
"n": 1,
"size": "1024x1024"
}'
描述越详细,你或者最终用户得到想要的结果的可能性越大。你可以探索DALL·E预览应用程序中的示例,以获取更多启发灵感。这里有一个快速的示例:
- a white siamese cat
- a close up, studio photographic portrait of a white siamese cat that looks curious, backlit ears
每个图像可以使用 response_format 参数作为URL或Base64数据返回。URL将在一小时后过期。
Edits
图像编辑端点允许您通过上传蒙版来编辑和扩展图像。蒙版的透明区域指示图像应该在哪里进行编辑,提示应该描述完整的新图像,而不仅仅是擦除的区域。这个端点可以实现类似于我们的 DALL·E 预览应用程序中的编辑器的体验。
curl https://api.openai.com/v1/images/edits \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F image="@sunlit_lounge.png" \
-F mask="@mask.png" \
-F prompt="A sunlit indoor lounge area with a pool containing a flamingo" \
-F n=1 \
-F size="1024x1024"
上传的图片和mask必须都是小于4MB的方形PNG图像,并且它们的尺寸必须相同。生成输出时不使用mask中的非透明区域,因此它们不一定需要像上面的示例那样与原始图像匹配。
Variations
The image variations endpoint allows you to generate a variation of a given image.
curl https://api.openai.com/v1/images/variations \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F image='@corgi_and_cat_paw.png' \
-F n=1 \
-F size="1024x1024"
Similar to the edits endpoint, the input image must be a square PNG image less than 4MB in size.
Content moderation
基于我们的内容政策,提示和图像将被过滤,如果提示或图片被标记,则会返回错误。如果您对误报或相关问题有任何反馈,请通过我们的帮助中心联系我们。
Language-specific tips
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:
# 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:
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
:
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)
创建图片 API
POST https://api.openai.com/v1/images/generations
Creates an image given a prompt.
Request body
prompt
- string
- Required
图片描述语,最大长度为1000个字符。
n
- integer
- Optional
- Defaults to 1
生成图片的数量,最少为 1,最大为 10 。
size
- string
- Optional
- Defaults to 1024x1024
The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
response_format
- string
- Optional
- Defaults to url
The format in which the generated images are returned. Must be one of url or b64_json.
user
- string
- Optional
一个独特的标识符,代表您的终端用户,可以帮助OpenAI监测和检测滥用情况。
例子
请求例子
curl
curl https://api.openai.com/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"prompt": "A cute baby sea otter",
"n": 2,
"size": "1024x1024"
}'
python
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Image.create(
prompt="A cute baby sea otter",
n=2,
size="1024x1024"
)
node.js
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createImage({
prompt: "A cute baby sea otter",
n: 2,
size: "1024x1024",
});
响应
{
"created": 1589478378,
"data": [
{
"url": "https://..."
},
{
"url": "https://..."
}
]
}
Create image edit
POST https://api.openai.com/v1/images/edits
Creates an edited or extended image given an original image and a prompt.
Request body
image
- string
- Required
The image to edit. Must be a valid PNG file, less than 4MB, and square. If mask is not provided, image must have transparency, which will be used as the mask.
mask
- string
- Optional
An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
prompt
- string
- Required
A text description of the desired image(s). The maximum length is 1000 characters.
n
- integer
- Optional
- Defaults to 1
The number of images to generate. Must be between 1 and 10.
size
- string
- Optional
- Defaults to 1024x1024
The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
response_format
- string
- Optional
- Defaults to url
The format in which the generated images are returned. Must be one of url or b64_json.
user
- string
- Optional
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.
例子
请求例子
curl
curl https://api.openai.com/v1/images/edits \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F image="@otter.png" \
-F mask="@mask.png" \
-F prompt="A cute baby sea otter wearing a beret" \
-F n=2 \
-F size="1024x1024"
Python
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Image.create_edit(
image=open("otter.png", "rb"),
mask=open("mask.png", "rb"),
prompt="A cute baby sea otter wearing a beret",
n=2,
size="1024x1024"
)
node.js
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createImageEdit(
fs.createReadStream("otter.png"),
fs.createReadStream("mask.png"),
"A cute baby sea otter wearing a beret",
2,
"1024x1024"
);
响应例子
{
"created": 1589478378,
"data": [
{
"url": "https://..."
},
{
"url": "https://..."
}
]
}
Create image variation
POST https://api.openai.com/v1/images/variations
Creates a variation of a given image.
Request body
image
- string
- Required
The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
n
- integer
- Optional
- Defaults to 1
The number of images to generate. Must be between 1 and 10.
size
- string
- Optional
- Defaults to 1024x1024
The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.
response_format
- string
- Optional
- Defaults to url
The format in which the generated images are returned. Must be one of url or b64_json.
user
- string
- Optional
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.
例子
请求例子
curl
curl https://api.openai.com/v1/images/variations \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F image="@otter.png" \
-F n=2 \
-F size="1024x1024"
Python
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Image.create_variation(
image=open("otter.png", "rb"),
n=2,
size="1024x1024"
)
node.js
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createImageVariation(
fs.createReadStream("otter.png"),
2,
"1024x1024"
);
响应例子
{
"created": 1589478378,
"data": [
{
"url": "https://..."
},
{
"url": "https://..."
}
]
}