Image generation Beta 圖片生成
Learn how to generate or manipulate images with our DALL·E models
了解如何使用我們的DALL·E模型生成或操作圖像
前言
ChatGPT不僅僅是一個(gè)強(qiáng)大的自然語(yǔ)言處理模型,其DALL·E模型還可以幫助用戶快速生成多種多樣的圖像。用戶可以根據(jù)文字快速生成圖片,即可自動(dòng)生成出特定的圖片,大大提高了工作效率。對(duì)于多媒體行業(yè)從業(yè)者來(lái)說(shuō),DALL·E模型強(qiáng)大的圖像處理能力,可以幫助用戶節(jié)省大量時(shí)間,讓用戶更加專注地精心制作自己的作品,從而提高工作效率。
Introduction
The Images API provides three methods for interacting with images:
圖像API提供了三種與圖像交互的方法:
- Creating images from scratch based on a text prompt
基于文本提示從頭開始創(chuàng)建圖像 - Creating edits of an existing image based on a new text prompt
基于新文本提示創(chuàng)建現(xiàn)有圖像的編輯 - Creating variations of an existing image
創(chuàng)建現(xiàn)有圖像的變體
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.
本指南涵蓋了使用這三個(gè)API端點(diǎn)的基礎(chǔ)知識(shí)和有用的代碼示例。要查看它們的實(shí)際操作,請(qǐng)查看我們的DALL·E預(yù)覽應(yīng)用程序。
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. You can learn more about rate limits in our rate limit guide.
圖像API處于beta階段。在此期間,API和模型將根據(jù)您的反饋進(jìn)行改進(jìn)。為了確保所有用戶都能輕松地進(jìn)行原型制作,默認(rèn)速率限制為每分鐘50張圖像。您可以在我們的費(fèi)率限制指南中了解有關(guān)費(fèi)率限制的更多信息。
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.
圖像生成端點(diǎn)允許您在給出文本提示的情況下創(chuàng)建原始圖像。生成的圖像可以具有256 x256、512 x512或1024 x1024像素的大小。較小的尺寸生成速度更快。您可以使用n參數(shù)一次請(qǐng)求1-10個(gè)圖像。
python代碼如下:
response = openai.Image.create(
prompt="a white siamese cat",
n=1,
size="1024x1024"
)
image_url = response['data'][0]['url']
node.js代碼如下:
const response = await openai.createImage({
prompt: "a white siamese cat",
n: 1,
size: "1024x1024",
});
image_url = response.data.data[0].url;
curl 代碼如下:
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"
}'
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:
描述越詳細(xì),就越有可能獲得您或最終用戶想要的結(jié)果。您可以在DALL·E預(yù)覽應(yīng)用程序中探索示例,以獲得更多提示靈感。這里有一個(gè)簡(jiǎn)單的例子:
PROMPT 提示
a white siamese cat 白色暹羅貓
GENERATION 產(chǎn)生
PROMPT 提示
a close up, studio photographic portrait of a white siamese cat that looks curious, backlit ears
一個(gè)近距離,工作室攝影肖像的白色暹羅貓,看起來(lái)好奇,背光的耳朵
GENERATION 產(chǎn)生
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參數(shù),每個(gè)圖像都可以作為URL或Base64數(shù)據(jù)返回。URL將在一小時(shí)后過(guò)期。
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.
圖像編輯端點(diǎn)允許您通過(guò)上傳遮罩來(lái)編輯和擴(kuò)展圖像。蒙版的透明區(qū)域指示應(yīng)該編輯圖像的位置,提示應(yīng)該描述完整的新圖像,而不僅僅是擦除的區(qū)域。此端點(diǎn)可以實(shí)現(xiàn)類似于DALL·E預(yù)覽應(yīng)用中的編輯器的體驗(yàn)。
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']
node.js代碼如下:
const response = await openai.createImageEdit(
fs.createReadStream("sunlit_lounge.png"),
fs.createReadStream("mask.png"),
"A sunlit indoor lounge area with a pool containing a flamingo",
1,
"1024x1024"
);
image_url = response.data.data[0].url;
curl 代碼如下:
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"
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圖像,并且彼此的尺寸必須相同。生成輸出時(shí)不使用蒙版的非透明區(qū)域,因此它們不一定需要像上面的示例那樣匹配原始圖像。
Variations
The image variations endpoint allows you to generate a variation of a given image.
圖像變體端點(diǎn)允許您生成給定圖像的變體。
python代碼如下:
response = openai.Image.create_variation(
image=open("corgi_and_cat_paw.png", "rb"),
n=1,
size="1024x1024"
)
image_url = response['data'][0]['url']
node.js代碼如下:
const response = await openai.createImageVariation(
fs.createReadStream("corgi_and_cat_paw.png"),
1,
"1024x1024"
);
image_url = response.data.data[0].url;
curl 代碼如下:
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"
Language-specific tips 特定語(yǔ)言提示
Python 語(yǔ)言
Using in-memory image data 使用內(nèi)存中的圖像數(shù)據(jù)
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
函數(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"
)
Operating on image data 操作圖像數(shù)據(jù)
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之前對(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"
)
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請(qǐng)求可能由于無(wú)效輸入、速率限制或其他問(wèn)題而返回錯(cuò)誤。這些錯(cuò)誤可以用 try...except
語(yǔ)句處理,錯(cuò)誤細(xì)節(jié)可以在 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)
Node.js 語(yǔ)言
Using in-memory image data 使用內(nèi)存中的圖像數(shù)據(jù)
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
模塊從磁盤讀取圖像數(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 這是包含圖像數(shù)據(jù)的Buffer對(duì)象
const buffer = [your image data];
// Set a `name` that ends with .png so that the API knows it's a PNG image 設(shè)置一個(gè)以.png結(jié)尾的“名稱”,這樣API就知道這是一個(gè)PNG圖像
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,你可能會(huì)遇到一些圖像文件參數(shù)的怪異。下面是一個(gè)通過(guò)顯式轉(zhuǎn)換參數(shù)來(lái)解決類型不匹配的示例:
// Cast the ReadStream to `any` to appease the TypeScript compiler 將ReadStream轉(zhuǎn)換為' any '以便TypeScript編譯器使用
const response = await openai.createImageVariation(
fs.createReadStream("image.png") as any,
1,
"1024x1024"
);
And here’s a similar example for in-memory image data:
下面是內(nèi)存中圖像數(shù)據(jù)的類似示例:
// This is the Buffer object that contains your image data 這是包含圖像數(shù)據(jù)的Buffer對(duì)象
const buffer: Buffer = [your image data];
// Cast the buffer to `any` so that we can set the `name` property 將緩沖區(qū)轉(zhuǎn)換為' any ',以便我們可以設(shè)置' name '屬性
const file: any = buffer;
// Set a `name` that ends with .png so that the API knows it's a PNG image 設(shè)置一個(gè)以.png結(jié)尾的“名稱”,這樣API就知道這是一個(gè)PNG圖像
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請(qǐng)求可能由于無(wú)效輸入、速率限制或其他問(wèn)題而返回錯(cuò)誤。這些錯(cuò)誤可以用 try...catch
語(yǔ)句處理,錯(cuò)誤詳細(xì)信息可以在error.response
或error.message
中找到:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-432872.html
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);
}
}
其它資料下載
如果大家想繼續(xù)了解人工智能相關(guān)學(xué)習(xí)路線和知識(shí)體系,歡迎大家翻閱我的另外一篇博客《重磅 | 完備的人工智能AI 學(xué)習(xí)——基礎(chǔ)知識(shí)學(xué)習(xí)路線,所有資料免關(guān)注免套路直接網(wǎng)盤下載》
這篇博客參考了Github知名開源平臺(tái),AI技術(shù)平臺(tái)以及相關(guān)領(lǐng)域?qū)<遥篋atawhale,ApacheCN,AI有道和黃海廣博士等約有近100G相關(guān)資料,希望能幫助到所有小伙伴們。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-432872.html
到了這里,關(guān)于OpenAI-ChatGPT最新官方接口《AI繪圖》全網(wǎng)最詳細(xì)中英文實(shí)用指南和教程,助你零基礎(chǔ)快速輕松掌握全新技術(shù)(三)(附源碼)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!