【OpenAI API】Images

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

给定一个提示和/或输入图像,该模型将生成一张新图像。

相关指引: 图片生成

Image generation

学习如何使用我们的 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和模型将根据您的反馈进行改进。为了确保所有用户都能轻松地进行原型设计, 默认速率限制为每分钟50张图片。如果您想增加速率限制,请查看此帮助中心文章。随着我们了解更多的使用和容量要求,我们将增加默认速率限制。

Usage

Generations

图像生成端点允许您根据给定的文本提示创建原始图像。生成的图像可以是256x256,512x512或1024x1024像素的大小。较小的尺寸生成速度更快。您可以使用“n”参数一次请求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"
  }'

描述越详细,你或者最终用户得到想要的结果的可能性越大。你可以探索DALL·E预览应用程序中的示例,以获取更多启发灵感。这里有一个快速的示例:

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

每个图像可以使用 response_format 参数作为URL或Base64数据返回。URL将在一小时后过期。

Edits

图像编辑端点允许您通过上传蒙版来编辑和扩展图像。蒙版的透明区域指示图像应该在哪里进行编辑,提示应该描述完整的新图像,而不仅仅是擦除的区域。这个端点可以实现类似于我们的 DALL·E 预览应用程序中的编辑器的体验。

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中的非透明区域,因此它们不一定需要像上面的示例那样与原始图像匹配。

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

基于我们的内容政策,提示和图像将被过滤,如果提示或图片被标记,则会返回错误。如果您对误报或相关问题有任何反馈,请通过我们的帮助中心联系我们。

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)

创建图片 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

生成图片的数量,最少为 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

一个独特的标识符,代表您的终端用户,可以帮助OpenAI监测和检测滥用情况。

例子

请求例子

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",
});

响应

{
  "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"
);

响应例子

{
  "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"
);

响应例子

{
  "created": 1589478378,
  "data": [
    {
      "url": "https://..."
    },
    {
      "url": "https://..."
    }
  ]
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342

推荐阅读更多精彩内容