【OpenAI API】Images

REF: https://platform.openai.com/docs/api-reference/images

給定一個提示和/或輸入圖像龙巨,該模型將生成一張新圖像旨别。

相關(guān)指引: 圖片生成

Image generation

學(xué)習(xí)如何使用我們的 DALL·E 模型生成或操作圖像

介紹

Images API 提供三種與圖像交互的方法:

  1. Creating images from scratch based on a text prompt
  2. Creating edits of an existing image based on a new text prompt
  3. 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://..."
    }
  ]
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末舷手,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子男窟,更是在濱河造成了極大的恐慌蝎宇,老刑警劉巖姥芥,帶你破解...
    沈念sama閱讀 211,561評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件凉唐,死亡現(xiàn)場離奇詭異霍骄,居然都是意外死亡读整,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,218評論 3 385
  • 文/潘曉璐 我一進店門强品,熙熙樓的掌柜王于貴愁眉苦臉地迎上來的榛,“玉大人,你說我怎么就攤上這事夫晌∠恚” “怎么了盏档?”我有些...
    開封第一講書人閱讀 157,162評論 0 348
  • 文/不壞的土叔 我叫張陵锄俄,是天一觀的道長奶赠。 經(jīng)常有香客問我药有,道長愤惰,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,470評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮奠旺,結(jié)果婚禮上响疚,老公的妹妹穿的比我還像新娘忿晕。我一直安慰自己践盼,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 65,550評論 6 385
  • 文/花漫 我一把揭開白布赖淤。 她就那樣靜靜地躺著咱旱,像睡著了一般吐限。 火紅的嫁衣襯著肌膚如雪褂始。 梳的紋絲不亂的頭發(fā)上诸典,一...
    開封第一講書人閱讀 49,806評論 1 290
  • 那天,我揣著相機與錄音崎苗,去河邊找鬼狐粱。 笑死舀寓,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的肌蜻。 我是一名探鬼主播互墓,決...
    沈念sama閱讀 38,951評論 3 407
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼蒋搜!你這毒婦竟也來了篡撵?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,712評論 0 266
  • 序言:老撾萬榮一對情侶失蹤豆挽,失蹤者是張志新(化名)和其女友劉穎帮哈,沒想到半個月后宿刮,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,166評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,510評論 2 327
  • 正文 我和宋清朗相戀三年斤富,在試婚紗的時候發(fā)現(xiàn)自己被綠了焕参。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,643評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖顷歌,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤澈蚌,帶...
    沈念sama閱讀 34,306評論 4 330
  • 正文 年R本政府宣布份汗,位于F島的核電站,受9級特大地震影響旁钧,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜寄猩,卻給世界環(huán)境...
    茶點故事閱讀 39,930評論 3 313
  • 文/蒙蒙 一斯辰、第九天 我趴在偏房一處隱蔽的房頂上張望彬呻。 院中可真熱鬧剪况,春花似錦或悲、人聲如沸翎蹈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,745評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至揩抡,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背装黑。 一陣腳步聲響...
    開封第一講書人閱讀 31,983評論 1 266
  • 我被黑心中介騙來泰國打工疚颊, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留材义,地道東北人油挥。 一個月前我還...
    沈念sama閱讀 46,351評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像负饲,于是被迫代替她去往敵國和親妥泉。 傳聞我的和親對象是個殘疾皇子蝇率,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,509評論 2 348

推薦閱讀更多精彩內(nèi)容