概述
OpenAI API 允许您使用 GPT 图像模型(包括我们最新的 gpt-image-2)根据文本提示生成和编辑图像。您可以通过两种 API 访问图像生成功能。
图像 API
从 gpt-image-1 及更高版本模型开始,图像 API 提供了两个端点,每个端点具有不同的功能。
图像 API 还包含一个针对支持它的模型(如 DALL·E 2)的变体端点。
Responses API
响应 API (Responses API) 允许您在对话或多步流程中生成图像。它支持将图像生成作为内置工具,并可在上下文中接收和输出图像。
与图像 API 相比,它增加了:
- 多轮编辑:通过提示词迭代地对图像进行高保真编辑。
- 灵活输入:支持以图像 文件 ID 作为输入图像,而不仅是字节数据。
响应 API 的图像生成工具使用其自己的 GPT 图像模型选择。关于支持调用此工具的主线模型的详细信息,请参阅下方的支持的模型。
选择合适的 API
- 如果您只需要根据一个提示词生成或编辑单张图像,图像 API 是您的最佳选择。
- 如果您想通过 GPT 图像构建对话式、可编辑的图像体验,请使用响应 API。
两种 API 都允许您通过调整质量、尺寸、格式和压缩率来自定义输出。透明背景取决于模型支持情况。
本指南重点介绍 GPT 图像。

生成图像
您可以使用图像生成端点根据文本提示创建图像,或使用响应 API 中的图像生成工具在对话过程中生成图像。
要了解有关自定义输出(尺寸、质量、格式、压缩)的更多信息,请参阅下方的自定义图像输出部分。
您可以设置 n 参数,在单次请求中同时生成多张图像(默认情况下,API 返回单张图像)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from openai import OpenAI
import base64
client = OpenAI()
response = client.responses.create(
model="gpt-5.5",
input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
tools=[{"type": "image_generation"}],
)
# Save the image to a file
image_data = [
output.result
for output in response.output
if output.type == "image_generation_call"
]
if image_data:
image_base64 = image_data[0]
with open("otter.png", "wb") as f:
f.write(base64.b64decode(image_base64))1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from openai import OpenAI
import base64
client = OpenAI()
prompt = """
A children's book drawing of a veterinarian using a stethoscope to
listen to the heartbeat of a baby otter.
"""
result = client.images.generate(
model="gpt-image-2",
prompt=prompt
)
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
# Save the image to a file
with open("otter.png", "wb") as f:
f.write(image_bytes)多轮图像生成
借助响应 API,您可以构建涉及图像生成的多轮对话。既可以在上下文中提供图像生成调用的输出(也可以直接使用图像 ID),也可以使用 previous_response_id 参数。这使您能够在多轮对话中对图像进行迭代——优化提示词、应用新指令,并随着对话的进行演变视觉输出。
使用响应 API 的图像生成工具,受支持的工具模型可以选择生成新图像还是编辑对话中已有的图像。可选的 action 参数可控制此行为:保持 action: "auto" 让模型自行决定;设置 action: "generate" 始终创建新图像;或设置 action: "edit" 以在图像存在于上下文中时强制进行编辑。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from openai import OpenAI
import base64
client = OpenAI()
response = client.responses.create(
model="gpt-5.5",
input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
tools=[{"type": "image_generation", "action": "generate"}],
)
# Save the image to a file
image_data = [
output.result
for output in response.output
if output.type == "image_generation_call"
]
if image_data:
image_base64 = image_data[0]
with open("otter.png", "wb") as f:
f.write(base64.b64decode(image_base64))如果您在上下文中未提供图像的情况下强制设置 edit,调用将返回错误。将 action 保留为 auto,让模型决定何时生成或编辑。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from openai import OpenAI
import base64
client = OpenAI()
response = client.responses.create(
model="gpt-5.5",
input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
tools=[{"type": "image_generation"}],
)
image_data = [
output.result
for output in response.output
if output.type == "image_generation_call"
]
if image_data:
image_base64 = image_data[0]
with open("cat_and_otter.png", "wb") as f:
f.write(base64.b64decode(image_base64))
# Follow up
response_fwup = client.responses.create(
model="gpt-5.5",
previous_response_id=response.id,
input="Now make it look realistic",
tools=[{"type": "image_generation"}],
)
image_data_fwup = [
output.result
for output in response_fwup.output
if output.type == "image_generation_call"
]
if image_data_fwup:
image_base64 = image_data_fwup[0]
with open("cat_and_otter_realistic.png", "wb") as f:
f.write(base64.b64decode(image_base64))1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import openai
import base64
response = openai.responses.create(
model="gpt-5.5",
input="Generate an image of gray tabby cat hugging an otter with an orange scarf",
tools=[{"type": "image_generation"}],
)
image_generation_calls = [
output
for output in response.output
if output.type == "image_generation_call"
]
image_data = [output.result for output in image_generation_calls]
if image_data:
image_base64 = image_data[0]
with open("cat_and_otter.png", "wb") as f:
f.write(base64.b64decode(image_base64))
# Follow up
response_fwup = openai.responses.create(
model="gpt-5.5",
input=[
{
"role": "user",
"content": [{"type": "input_text", "text": "Now make it look realistic"}],
},
{
"type": "image_generation_call",
"id": image_generation_calls[0].id,
},
],
tools=[{"type": "image_generation"}],
)
image_data_fwup = [
output.result
for output in response_fwup.output
if output.type == "image_generation_call"
]
if image_data_fwup:
image_base64 = image_data_fwup[0]
with open("cat_and_otter_realistic.png", "wb") as f:
f.write(base64.b64decode(image_base64))结果
“生成一张灰色虎斑猫抱着一条围着橙色围巾的水獭的图片” | ![]() |
“现在让它看起来更真实” | ![]() |
流式传输
响应 API 和图像 API 支持流式图像生成。您可以在 API 生成图像时流式传输部分图像,从而提供更具交互性的体验。
您可以调整 partial_images 参数以接收 0-3 个部分图像。
- 如果您将
partial_images设置为 0,则只会收到最终图像。 - 对于大于零的值,如果最终图像生成速度较快,您可能不会收到所请求的全部数量的部分图像。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from openai import OpenAI
import base64
client = OpenAI()
stream = client.responses.create(
model="gpt-5.5",
input="Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape",
stream=True,
tools=[{"type": "image_generation", "partial_images": 2}],
)
for event in stream:
if event.type == "response.image_generation_call.partial_image":
idx = event.partial_image_index
image_base64 = event.partial_image_b64
image_bytes = base64.b64decode(image_base64)
with open(f"river{idx}.png", "wb") as f:
f.write(image_bytes)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from openai import OpenAI
import base64
client = OpenAI()
stream = client.images.generate(
prompt="Draw a gorgeous image of a river made of white owl feathers, snaking its way through a serene winter landscape",
model="gpt-image-2",
stream=True,
partial_images=2,
)
for event in stream:
if event.type == "image_generation.partial_image":
idx = event.partial_image_index
image_base64 = event.b64_json
image_bytes = base64.b64decode(image_base64)
with open(f"river{idx}.png", "wb") as f:
f.write(image_bytes)结果
| 部分 1 | 部分 2 | 最终图像 |
|---|---|---|
![]() | ![]() | ![]() |
提示词:画一幅华丽的画,河水由白色猫头鹰羽毛组成,蜿蜒穿过宁静的冬日景观
修订后的提示词
在使用响应 API 中的图像生成工具时,主线模型(例如 gpt-5.5)将自动修订您的提示词以获得更好的表现。
您可以在图像生成调用的 revised_prompt 字段中访问修订后的提示词。
1
2
3
4
5
6
7
{
"id": "ig_123",
"type": "image_generation_call",
"status": "completed",
"revised_prompt": "A gray tabby cat hugging an otter. The otter is wearing an orange scarf. Both animals are cute and friendly, depicted in a warm, heartwarming style.",
"result": "..."
}编辑图像
图像编辑端点允许您:
- 编辑现有图像
- 使用其他图像作为参考生成新图像
- 通过上传图像和识别待替换区域的遮罩 (mask) 来编辑图像的局部
使用图像参考创建新图像
您可以使用一张或多张图像作为参考来生成新图像。
在本例中,我们将使用 4 张输入图像生成一张包含参考图像中物品的礼品篮新图。
使用响应 API,您可以通过 3 种不同方式提供输入图像:
- 提供完整的 URL
- 将图像作为 Base64 编码的数据 URL 提供
- 提供文件 ID(使用 文件 API 创建)
创建文件
1
2
3
4
5
6
7
8
9
10
from openai import OpenAI
client = OpenAI()
def create_file(file_path):
with open(file_path, "rb") as file_content:
result = client.files.create(
file=file_content,
purpose="vision",
)
return result.id创建 Base64 编码的图像
1
2
3
4
def encode_image(file_path):
with open(file_path, "rb") as f:
base64_image = base64.b64encode(f.read()).decode("utf-8")
return base64_image1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from openai import OpenAI
import base64
client = OpenAI()
prompt = """Generate a photorealistic image of a gift basket on a white background
labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
containing all the items in the reference pictures."""
base64_image1 = encode_image("body-lotion.png")
base64_image2 = encode_image("soap.png")
file_id1 = create_file("body-lotion.png")
file_id2 = create_file("incense-kit.png")
response = client.responses.create(
model="gpt-5.5",
input=[
{
"role": "user",
"content": [
{"type": "input_text", "text": prompt},
{
"type": "input_image",
"image_url": f"data:image/jpeg;base64,{base64_image1}",
},
{
"type": "input_image",
"image_url": f"data:image/jpeg;base64,{base64_image2}",
},
{
"type": "input_image",
"file_id": file_id1,
},
{
"type": "input_image",
"file_id": file_id2,
}
],
}
],
tools=[{"type": "image_generation"}],
)
image_generation_calls = [
output
for output in response.output
if output.type == "image_generation_call"
]
image_data = [output.result for output in image_generation_calls]
if image_data:
image_base64 = image_data[0]
with open("gift-basket.png", "wb") as f:
f.write(base64.b64decode(image_base64))
else:
print(response.output.content)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import base64
from openai import OpenAI
client = OpenAI()
prompt = """
Generate a photorealistic image of a gift basket on a white background
labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
containing all the items in the reference pictures.
"""
result = client.images.edit(
model="gpt-image-2",
image=[
open("body-lotion.png", "rb"),
open("bath-bomb.png", "rb"),
open("incense-kit.png", "rb"),
open("soap.png", "rb"),
],
prompt=prompt
)
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
# Save the image to a file
with open("gift-basket.png", "wb") as f:
f.write(image_bytes)使用遮罩编辑图像
您可以提供一个遮罩来指示图像的哪一部分应被编辑。
当结合 GPT 图像使用遮罩时,额外的指令会被发送给模型,以帮助引导编辑过程。
使用 GPT 图像进行遮罩处理完全基于提示词。模型将遮罩作为参考,但可能无法以绝对精确的方式遵循其确切形状。
如果您提供多个输入图像,遮罩将应用于第一张图像。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from openai import OpenAI
client = OpenAI()
fileId = create_file("sunlit_lounge.png")
maskId = create_file("mask.png")
response = client.responses.create(
model="gpt-5.5",
input=[
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "generate an image of the same sunlit indoor lounge area with a pool but the pool should contain a flamingo",
},
{
"type": "input_image",
"file_id": fileId,
}
],
},
],
tools=[
{
"type": "image_generation",
"quality": "high",
"input_image_mask": {
"file_id": maskId,
}
},
],
)
image_data = [
output.result
for output in response.output
if output.type == "image_generation_call"
]
if image_data:
image_base64 = image_data[0]
with open("lounge.png", "wb") as f:
f.write(base64.b64decode(image_base64))1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from openai import OpenAI
client = OpenAI()
result = client.images.edit(
model="gpt-image-2",
image=open("sunlit_lounge.png", "rb"),
mask=open("mask.png", "rb"),
prompt="A sunlit indoor lounge area with a pool containing a flamingo"
)
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
# Save the image to a file
with open("composition.png", "wb") as f:
f.write(image_bytes)| 图像 | 遮罩 | 输出 |
|---|---|---|
![]() | ![]() | ![]() |
提示词:一个阳光充足的室内休息区,带有一个装有火烈鸟的游泳池
遮罩要求
待编辑图像和遮罩必须具有相同的格式和尺寸(文件大小小于 50MB)。
遮罩图像还必须包含 alpha 通道。如果您使用图像编辑工具创建遮罩,请确保保存时带有 alpha 通道。
您可以以编程方式修改黑白图像以添加 alpha 通道。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from PIL import Image
from io import BytesIO
# 1. Load your black & white mask as a grayscale image
mask = Image.open(img_path_mask).convert("L")
# 2. Convert it to RGBA so it has space for an alpha channel
mask_rgba = mask.convert("RGBA")
# 3. Then use the mask itself to fill that alpha channel
mask_rgba.putalpha(mask)
# 4. Convert the mask into bytes
buf = BytesIO()
mask_rgba.save(buf, format="PNG")
mask_bytes = buf.getvalue()
# 5. Save the resulting file
img_path_mask_alpha = "mask_alpha.png"
with open(img_path_mask_alpha, "wb") as f:
f.write(mask_bytes)图像输入保真度
input_fidelity 参数控制模型在编辑和参考图像工作流程中保留输入图像细节的强度。对于 gpt-image-2,请省略此参数;API 不允许更改它,因为该模型会自动以高保真度处理每个图像输入。
由于 gpt-image-2 始终以高保真度处理图像输入,因此包含参考图像的编辑请求的图像输入 Token 可能会更高。要了解成本影响,请参阅视觉成本部分。
自定义图像输出
您可以配置以下输出选项:
- 尺寸 (Size):图像尺寸(例如
1024x1024,1024x1536) - 质量 (Quality):渲染质量(例如
low,medium,high) - 格式 (Format):文件输出格式
- 压缩 (Compression):JPEG 和 WebP 格式的压缩级别 (0-100%)
- 背景 (Background):不透明或自动
size, quality 和 background 支持 auto 选项,模型将根据提示词自动选择最佳设置。
gpt-image-2 目前不支持透明背景。对于此模型,不支持设置 background: "transparent" 的请求。
尺寸和质量选项
当满足以下约束条件时,gpt-image-2 在 size 参数中接受任何分辨率。方形图像通常生成速度最快。
| 常用尺寸 |
|
| 尺寸约束 |
|
| 质量选项 |
|
对于快速草图、缩略图和快速迭代,使用 quality: "low"。这是最快的选项,在进入 medium 或 high 以生成最终成品之前,它适用于许多常见用例。
总像素超过 2560x1440 (3,686,400) 的输出(通常称为 2K)被视为实验性功能。
输出格式
图像 API 返回 base64 编码的图像数据。默认格式为 png,但您也可以请求 jpeg 或 webp。
如果使用 jpeg 或 webp,您还可以指定 output_compression 参数来控制压缩级别 (0-100%)。例如,output_compression=50 会将图像压缩 50%。
使用 jpeg 比 png 更快,因此如果对延迟有要求,应优先考虑此格式。
限制
GPT 图像模型(gpt-image-2, gpt-image-1.5, gpt-image-1 和 gpt-image-1-mini)功能强大且用途广泛,但仍有一些局限性需要注意:
- 延迟: 复杂的提示词可能需要长达 2 分钟的处理时间。
- 文本渲染: 虽然有了显著改进,但模型在精确放置文本和清晰度方面仍可能存在困难。
- 一致性: 尽管能够产生连贯的图像,但在跨多次生成保持重复角色或品牌元素的一致性方面,模型偶尔会遇到挑战。
- 构图控制: 尽管指令遵循能力有所提高,但模型在结构化或对布局敏感的构图中精确放置元素时可能会有困难。
内容审核
所有提示词和生成的图像均根据我们的内容政策进行过滤。
对于使用 GPT 图像模型(gpt-image-2, gpt-image-1.5, gpt-image-1 和 gpt-image-1-mini)进行的图像生成,您可以使用 moderation 参数控制审核严格程度。此参数支持两个值:
auto(默认):标准过滤,旨在限制创建某些类别的潜在年龄不适宜内容。low:限制较少的过滤。
支持的模型
在使用响应 API 进行图像生成时,gpt-5 及更高版本的模型应支持图像生成工具。请查看您所用模型的详细页面以确认该模型是否可以使用图像生成工具。
成本与延迟
gpt-image-2 输出 Token
对于 gpt-image-2,请使用计算器根据所需的 quality 和 size 来估算输出 Token。
gpt-image-2 之前的模型
在 gpt-image-2 之前的 GPT 图像模型通过首先生成专门的图像 Token 来生成图像。延迟和最终成本都与渲染图像所需的 Token 数量成正比——较大的图像尺寸和较高的质量设置会导致更多的 Token。
生成的 Token 数量取决于图像尺寸和质量:
| 质量 | 正方形 (1024×1024) | 竖屏 (1024×1536) | 横屏 (1536×1024) |
|---|---|---|---|
| 低 | 272 Token | 408 Token | 400 Token |
| 中等 | 1056 Token | 1584 Token | 1568 Token |
| 高 | 4160 Token | 6240 Token | 6208 Token |
请注意,您还需要考虑输入 Token:提示词的文本 Token,以及编辑图像时的输入图像的图像 Token。由于 gpt-image-2 始终以高保真度处理图像输入,包含参考图像的编辑请求可能会消耗更多的输入 Token。
请参考定价页面了解当前的文本和图像 Token 价格,并使用下方的计算成本部分来估算请求成本。
最终成本是以下各项的总和:
- 输入文本 Token
- 使用编辑端点时的输入图像 Token
- 图像输出 Token
计算成本
使用下方的定价计算器来估算 GPT 图像模型的请求成本。gpt-image-2 支持数千种有效分辨率;下表列出了与以前的 GPT 图像模型相同的尺寸以供比较。对于 GPT 图像 1.5、GPT 图像 1 和 GPT 图像 1 Mini,下方也列出了旧版的每张图像输出定价表。在估算请求总成本时,您仍应计入文本和图像输入 Token。
较大的非正方形分辨率有时在相同的质量设置下产生的输出 Token 比较小或正方形的分辨率更少。
| 模型 | 质量 | 1024 x 1024 | 1024 x 1536 | 1536 x 1024 |
|---|---|---|---|---|
GPT Image 2 可用的其他尺寸 | 低 | $0.006 | $0.005 | $0.005 |
| 中等 | $0.053 | $0.041 | $0.041 | |
| 高 | $0.211 | $0.165 | $0.165 | |
GPT 图像 1.5 | 低 | $0.009 | $0.013 | $0.013 |
| 中等 | $0.034 | $0.05 | $0.05 | |
| 高 | $0.133 | $0.2 | $0.2 | |
GPT 图像 1 | 低 | $0.011 | $0.016 | $0.016 |
| 中等 | $0.042 | $0.063 | $0.063 | |
| 高 | $0.167 | $0.25 | $0.25 | |
GPT 图像 1 Mini | 低 | $0.005 | $0.006 | $0.006 |
| 中等 | $0.011 | $0.015 | $0.015 | |
| 高 | $0.036 | $0.052 | $0.052 |
部分图像成本
如果您想使用 partial_images 参数流式传输图像生成,每个部分图像将额外产生 100 个图像输出 Token。



























