REF: https://platform.openai.com/docs/api-reference/images
給定一個提示和/或輸入圖像龙巨,該模型將生成一張新圖像旨别。
相關(guān)指引: 圖片生成
Image generation
學(xué)習(xí)如何使用我們的 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和模型將根據(jù)您的反饋進行改進胆屿。為了確保所有用戶都能輕松地進行原型設(shè)計非迹, 默認速率限制為每分鐘50張圖片。如果您想增加速率限制冷离,請查看此幫助中心文章纯命。隨著我們了解更多的使用和容量要求亿汞,我們將增加默認速率限制。
Usage
Generations
圖像生成端點允許您根據(jù)給定的文本提示創(chuàng)建原始圖像咆畏。生成的圖像可以是256x256吴裤,512x512或1024x1024像素的大小麦牺。較小的尺寸生成速度更快。您可以使用“n”參數(shù)一次請求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"
}'
描述越詳細,你或者最終用戶得到想要的結(jié)果的可能性越大易结。你可以探索DALL·E預(yù)覽應(yīng)用程序中的示例搞动,以獲取更多啟發(fā)靈感渣刷。這里有一個快速的示例:
- a white siamese cat
- a close up, studio photographic portrait of a white siamese cat that looks curious, backlit ears
每個圖像可以使用 response_format 參數(shù)作為URL或Base64數(shù)據(jù)返回辅柴。URL將在一小時后過期。
Edits
圖像編輯端點允許您通過上傳蒙版來編輯和擴展圖像涣旨。蒙版的透明區(qū)域指示圖像應(yīng)該在哪里進行編輯霹陡,提示應(yīng)該描述完整的新圖像止状,而不僅僅是擦除的區(qū)域怯疤。這個端點可以實現(xiàn)類似于我們的 DALL·E 預(yù)覽應(yīng)用程序中的編輯器的體驗集峦。
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中的非透明區(qū)域凯沪,因此它們不一定需要像上面的示例那樣與原始圖像匹配妨马。
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
基于我們的內(nèi)容政策烘跺,提示和圖像將被過濾,如果提示或圖片被標(biāo)記梧喷,則會返回錯誤铺敌。如果您對誤報或相關(guān)問題有任何反饋屁擅,請通過我們的幫助中心聯(lián)系我們派歌。
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)
創(chuàng)建圖片 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
生成圖片的數(shù)量,最少為 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
一個獨特的標(biāo)識符趟佃,代表您的終端用戶昧捷,可以幫助OpenAI監(jiān)測和檢測濫用情況靡挥。
例子
請求例子
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",
});
響應(yīng)
{
"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"
);
響應(yīng)例子
{
"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"
);
響應(yīng)例子
{
"created": 1589478378,
"data": [
{
"url": "https://..."
},
{
"url": "https://..."
}
]
}