圖片 API 提供了三種與圖片交互的方法:
根據(jù)文本提示從頭開始創(chuàng)建圖像
根據(jù)新文本提示創(chuàng)建現(xiàn)有圖像的編輯
創(chuàng)建現(xiàn)有圖像的變體
本指南涵蓋了使用這三個(gè) API 端點(diǎn)的基礎(chǔ)知識(shí)以及有用的代碼示例。
圖片 API 處于測(cè)試階段。在此期間,API 和模型將根據(jù)您的反饋進(jìn)行改進(jìn)。為確保所有用戶都能輕松制作原型,默認(rèn)速率限制為每分鐘 50 張圖像。如果您想提高速率限制,請(qǐng)查看這篇幫助中心文章。隨著我們對(duì)使用和容量要求的更多了解,我們將提高默認(rèn)速率限制。
圖像生成端點(diǎn)允許您在給定文本提示的情況下創(chuàng)建原始圖像。生成的圖像的大小可以為 256x256、512x512 或 1024x1024 像素。較小的尺寸生成速度更快。您可以使用 n 參數(shù)一次請(qǐng)求 1-10 張圖像。
python | node.js | curl |
|
|
|
描述越詳細(xì),您就越有可能獲得您或您的最終用戶想要的結(jié)果。您可以探索 DALL·E 預(yù)覽應(yīng)用程序中的示例以獲得更多提示靈感。這是一個(gè)簡(jiǎn)單的例子:
使用 response_format 參數(shù),每個(gè)圖像都可以作為 URL 或 Base64 數(shù)據(jù)返回。 URL 將在一小時(shí)后過期。
圖像編輯端點(diǎn)允許您通過上傳蒙版來編輯和擴(kuò)展圖像。遮罩的透明區(qū)域指示應(yīng)編輯圖像的位置,提示應(yīng)描述完整的新圖像,而不僅僅是擦除區(qū)域。此端點(diǎn)可以啟用類似我們 DALL·E 預(yù)覽應(yīng)用程序中的編輯器的體驗(yàn)。
python | node.js | curl |
|
|
|
上傳的圖片和遮罩必須是小于 4MB 的正方形 PNG 圖片,并且必須具有相同的尺寸。生成輸出時(shí)不使用遮罩的非透明區(qū)域,因此它們不一定需要像上面的示例那樣與原始圖像匹配。
圖像變體端點(diǎn)允許您生成給定圖像的變體。
python | node.js | curl |
|
|
|
與編輯端點(diǎn)類似,輸入圖像必須是大小小于 4MB 的方形 PNG 圖像。
內(nèi)容審核
提示和圖像根據(jù)我們的內(nèi)容政策進(jìn)行過濾,當(dāng)提示或圖像被標(biāo)記時(shí)返回錯(cuò)誤。
特定語言提示
使用內(nèi)存圖像數(shù)據(jù)
上面指南中的 Node.js 示例使用 fs 模塊從磁盤讀取圖像數(shù)據(jù)。在某些情況下,您可能會(huì)將圖像數(shù)據(jù)保存在內(nèi)存中。下面是一個(gè)使用存儲(chǔ)在 Node.js Buffer 對(duì)象中的圖像數(shù)據(jù)的 API 調(diào)用示例:
// 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,您可能會(huì)遇到一些圖像文件參數(shù)的問題。下面是通過顯式轉(zhuǎn)換參數(shù)來解決類型不匹配的示例:
// Cast the ReadStream to `any` to appease the TypeScript compiler
const response = await openai.createImageVariation(
fs.createReadStream("image.png") as any,
1,
"1024x1024"
);
這是內(nèi)存中圖像數(shù)據(jù)的類似示例:
// 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"
);
錯(cuò)誤處理
API 請(qǐng)求可能會(huì)由于無效輸入、速率限制或其他問題而返回錯(cuò)誤。這些錯(cuò)誤可以用 try...catch 語句處理,錯(cuò)誤詳細(xì)信息可以在 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);
}
}
使用內(nèi)存圖像數(shù)據(jù)
上面指南中的 Python 示例使用 open 函數(shù)從磁盤讀取圖像數(shù)據(jù)。在某些情況下,您可能會(huì)將圖像數(shù)據(jù)保存在內(nèi)存中。下面是一個(gè)使用存儲(chǔ)在 BytesIO 對(duì)象中的圖像數(shù)據(jù)的 API 調(diào)用示例:
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"
)
圖像數(shù)據(jù)操作
在將圖像傳遞給 API 之前對(duì)圖像執(zhí)行操作可能很有用。這是一個(gè)使用 PIL 調(diào)整圖像大小的示例:
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"
)
API 請(qǐng)求可能會(huì)由于無效輸入、速率限制或其他問題而返回錯(cuò)誤。這些錯(cuò)誤可以通過 try...except 語句來處理,錯(cuò)誤的詳細(xì)信息可以在 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)
更多建議: