【OpenAI API】Images

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

給定一個(gè)提示和/或輸入圖像,該模型將生成一張新圖像。

相關(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目前處于測(cè)試階段。在此期間,API和模型將根據(jù)您的反饋進(jìn)行改進(jìn)。為了確保所有用戶都能輕松地進(jìn)行原型設(shè)計(jì), 默認(rèn)速率限制為每分鐘50張圖片。如果您想增加速率限制,請(qǐng)查看此幫助中心文章。隨著我們了解更多的使用和容量要求,我們將增加默認(rèn)速率限制。

Usage

Generations

圖像生成端點(diǎn)允許您根據(jù)給定的文本提示創(chuàng)建原始圖像。生成的圖像可以是256x256,512x512或1024x1024像素的大小。較小的尺寸生成速度更快。您可以使用“n”參數(shù)一次請(qǐng)求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"
  }'

描述越詳細(xì),你或者最終用戶得到想要的結(jié)果的可能性越大。你可以探索DALL·E預(yù)覽應(yīng)用程序中的示例,以獲取更多啟發(fā)靈感。這里有一個(gè)快速的示例:

  • a white siamese cat
  • a close up, studio photographic portrait of a white siamese cat that looks curious, backlit ears

每個(gè)圖像可以使用 response_format 參數(shù)作為URL或Base64數(shù)據(jù)返回。URL將在一小時(shí)后過期。

Edits

圖像編輯端點(diǎn)允許您通過上傳蒙版來(lái)編輯和擴(kuò)展圖像。蒙版的透明區(qū)域指示圖像應(yīng)該在哪里進(jìn)行編輯,提示應(yīng)該描述完整的新圖像,而不僅僅是擦除的區(qū)域。這個(gè)端點(diǎn)可以實(shí)現(xiàn)類似于我們的 DALL·E 預(yù)覽應(yīng)用程序中的編輯器的體驗(yàn)。

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圖像,并且它們的尺寸必須相同。生成輸出時(shí)不使用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)記,則會(huì)返回錯(cuò)誤。如果您對(duì)誤報(bào)或相關(guān)問題有任何反饋,請(qǐng)通過我們的幫助中心聯(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

圖片描述語(yǔ),最大長(zhǎng)度為1000個(gè)字符。

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

一個(gè)獨(dú)特的標(biāo)識(shí)符,代表您的終端用戶,可以幫助OpenAI監(jiān)測(cè)和檢測(cè)濫用情況。

例子

請(qǐng)求例子

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.

例子

請(qǐng)求例子

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.

例子

請(qǐng)求例子

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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容