概览
在本指南中,您将了解如何使用 OpenAI API 构建涉及图像的应用程序。如果您已经知道自己想构建什么,请在下方找到您的用例并立即开始。如果您不确定从何开始,请继续阅读以获取概览。
图像相关用例概览
最新的语言模型能够处理图像输入并对其进行分析——这一能力被称为 视觉。GPT 图像模型可以使用文本和图像输入来创建新图像或编辑现有图像。
OpenAI API 提供了多个端点,用于将图像作为输入进行处理或作为输出生成,使您能够构建强大的多模态应用程序。
| API | 支持的用例 |
|---|---|
| Responses API | 分析图像并将其作为输入和/或生成图像作为输出 |
| Images API | 将图像作为输出生成,可选将图像作为输入 |
| Chat Completions API | 分析图像并将其作为输入以生成文本或音频 |
要了解有关我们模型支持的输入和输出模态的更多信息,请参阅我们的 模型页面.
生成或编辑图像
您可以使用 Image API 或 Responses API 来生成或编辑图像。
最先进的图像生成模型, gpt-image-2, 能够理解文本和图像,并利用广泛的世界知识生成具有强大指令遵循能力和上下文感知能力的图像。
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-4.1-mini",
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("cat_and_otter.png", "wb") as f:
f.write(base64.b64decode(image_base64))您可以在我们的 图像生成 guide.
利用世界知识生成图像
GPT 图像模型能够利用对世界的视觉理解,在无需参考的情况下生成包含真实细节的逼真图像。
例如,如果您提示 GPT 图像生成一个装有最受欢迎半宝石的玻璃柜图像,该模型具备足够的知识来挑选紫水晶、粉水晶、玉石等,并以写实的方式描绘它们。
分析图像
视觉 是指模型“看见”并理解图像的能力。如果图像中有文本,模型也可以理解这些文本。它能够理解大多数视觉元素,包括物体、形状、颜色和纹理,即使存在某些 限制.
将图像作为输入提供给模型
您可以通过提供图像文件的完整 URL,或提供 Base64 编码的数据 URL,将图像作为输入提供给生成请求。
您可以在单个请求中通过在 content 数组中包含多张图像来提供多个图像作为输入,但请注意, 图像按 Token 计费 并将相应地进行收费。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{
"type": "image_url",
"image_url": {
"url": "https://api.nga.gov/iiif/a2e6da57-3cd1-4235-b20e-95dcaefed6c8/full/!800,800/0/default.jpg",
},
},
],
}],
)
print(response.choices[0].message.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
28
29
30
31
32
33
34
35
36
import base64
from openai import OpenAI
client = OpenAI()
# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
# Path to your image
image_path = "path_to_your_image.jpg"
# Getting the Base64 string
base64_image = encode_image(image_path)
completion = client.chat.completions.create(
model="gpt-4.1",
messages=[
{
"role": "user",
"content": [
{ "type": "text", "text": "what's in this image?" },
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}",
},
},
],
}
],
)
print(completion.choices[0].message.content)您可以通过多种方式将图像作为输入提供给生成请求:
- 提供图像文件的完整 URL
- 提供 Base64 编码的 Data URL 格式图片
- 提供文件 ID(通过 Files API)
您可以在单个请求中通过在 content 数组中包含多张图像来提供多个图像作为输入,但请注意, 图像按 Token 计费 并将相应地进行收费。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1-mini",
input=[{
"role": "user",
"content": [
{"type": "input_text", "text": "what's in this image?"},
{
"type": "input_image",
"image_url": "https://api.nga.gov/iiif/a2e6da57-3cd1-4235-b20e-95dcaefed6c8/full/!800,800/0/default.jpg",
},
],
}],
)
print(response.output_text)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
import base64
from openai import OpenAI
client = OpenAI()
# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
# Path to your image
image_path = "path_to_your_image.jpg"
# Getting the Base64 string
base64_image = encode_image(image_path)
response = client.responses.create(
model="gpt-4.1",
input=[
{
"role": "user",
"content": [
{ "type": "input_text", "text": "what's in this image?" },
{
"type": "input_image",
"image_url": f"data:image/jpeg;base64,{base64_image}",
},
],
}
],
)
print(response.output_text)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
from openai import OpenAI
client = OpenAI()
# Function to create a file with the Files API
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
# Getting the file ID
file_id = create_file("path_to_your_image.jpg")
response = client.responses.create(
model="gpt-4.1-mini",
input=[{
"role": "user",
"content": [
{"type": "input_text", "text": "what's in this image?"},
{
"type": "input_image",
"file_id": file_id,
},
],
}],
)
print(response.output_text)图像输入要求
输入图像必须满足以下要求才能在 API 中使用。
| 支持的文件类型 |
|
| 大小限制 |
|
| 其他要求 |
|
选择图像细节级别
The detail 参数告诉模型在处理和理解图像时使用何种级别的细节 (low, high, original, or auto)。如果您省略该参数,模型将使用 auto。此行为在 Responses API 和 Chat Completions API 中相同。在 gpt-5.5, auto ,并且默认的省略行为等同于 original.
1
2
3
4
"image_url": {
"url": "https://api.nga.gov/iiif/a2e6da57-3cd1-4235-b20e-95dcaefed6c8/full/!800,800/0/default.jpg",
"detail": "original"
},1
2
3
4
5
{
"type": "input_image",
"image_url": "https://api.nga.gov/iiif/a2e6da57-3cd1-4235-b20e-95dcaefed6c8/full/!800,800/0/default.jpg",
"detail": "original"
}请使用以下指南来选择细节级别:
| 细节级别 | 适用场景 |
|---|---|
low | 当不需要精细的视觉细节时,提供快速、低成本的理解。模型将接收图像的低分辨率 512px x 512px 版本。 |
high | 标准的高保真图像理解。 |
original | 大型、密集、对空间敏感或用于计算机操作的图像。适用于 gpt-5.4 and future models. |
auto | 自动细节选择。在 gpt-5.5, auto 上与省略/默认行为等效 original. |
对于在 gpt-5.4 及未来模型上的计算机操作、定位和点击精准度用例,我们建议 "detail": "original"。请参阅 计算机操作指南 for more detail.
在 章节详细了解模型如何调整图像大小,并在 章节了解关于 token 成本的更多信息,并在 计算成本 部分。
章节详细了解模型如何调整图像大小,并在
不同模型在图像 token 化之前会使用不同的缩放规则:
| 模型系列 | 支持的细节级别 | 分块与缩放行为 |
|---|---|---|
gpt-5.5 |
|
|
gpt-5.4 |
|
|
|
|
|
|
| 使用基于分块的缩放行为。参见 the detailed behavior below |
计算成本
图像输入按 token 单位计量和收费,类似于文本输入。图像如何转换为文本 token 输入因模型而异。你可以在以下内容的常见问题解答 (FAQ) 部分找到视觉定价计算器: 中找到.
基于分块的图像 token 化
某些模型通过使用 32px x 32px 的分块覆盖图像来对其进行 token 化。每个模型都定义了最大分块预算。图像的 token 成本计算如下:
A. 计算覆盖原始图像需要多少个 32px x 32px 的分块。分块可以超出图像边界。
original_patch_count = ceil(width/32)×ceil(height/32)B. 如果原始图像将超出模型的分块预算,则按比例缩小,直到其符合该预算。然后调整比例,以便在转换为整数像素尺寸并计算分块覆盖范围后,最终调整过大小的图像仍保持在预算内。
shrink_factor = sqrt((32^2 * patch_budget) / (width * height))
adjusted_shrink_factor = shrink_factor * min(
floor(width * shrink_factor / 32) / (width * shrink_factor / 32),
floor(height * shrink_factor / 32) / (height * shrink_factor / 32)
)C. 将调整后的比例转换为整数像素尺寸,然后计算覆盖调整大小后的图像所需的分块数量。此调整大小后的分块数量即为应用模型乘数之前的图像 token 数,并且它受限于模型的分块预算。
resized_patch_count = ceil(resized_width/32)×ceil(resized_height/32)D. 应用基于模型的乘数来获取总 token 数:
| 模型 | 乘数 |
|---|---|
gpt-5.4-mini | 1.62 |
gpt-5.4-nano | 2.46 |
gpt-5-mini | 1.62 |
gpt-5-nano | 2.46 |
gpt-4.1-mini* | 1.62 |
gpt-4.1-nano* | 2.46 |
o4-mini | 1.72 |
For gpt-4.1-mini and gpt-4.1-nano, 之外的 o 系列模型,这适用于 2025-04-14 快照变体。
具有 1,536 分块预算的模型的成本计算示例
- 一张 1024 x 1024 的图像调整大小后的分块数量为 1024
- A.
original_patch_count = ceil(1024 / 32) * ceil(1024 / 32) = 32 * 32 = 1024 - B.
1024低于1,536patch 预算,因此无需调整大小。 - C.
resized_patch_count = 1024 - 模型乘数应用前的重设大小后 patch 数量:
1024 - 乘以模型的 token 乘数,即可得出计费的 token 单位数。
- A.
- 一张 1800 x 2400 像素的图像,其重设大小后的 patch 数为 1452
- A.
original_patch_count = ceil(1800 / 32) * ceil(2400 / 32) = 57 * 75 = 4275 - B.
4275超出1,536patch 预算,因此我们首先计算shrink_factor = sqrt((32^2 * 1536) / (1800 * 2400)) = 0.603. - 随后我们调整该缩放比例,以确保最终的整数像素尺寸在经过 patch 计数后仍保持在预算范围内:
adjusted_shrink_factor = 0.603 * min(floor(1800 * 0.603 / 32) / (1800 * 0.603 / 32), floor(2400 * 0.603 / 32) / (2400 * 0.603 / 32)) = 0.586. - 以整数像素计的重设大小后图像:
1056 x 1408 - C.
resized_patch_count = ceil(1056 / 32) * ceil(1408 / 32) = 33 * 44 = 1452 - 模型乘数应用前的重设大小后 patch 数量:
1452 - 乘以模型的 token 乘数,即可得出计费的 token 单位数。
- A.
基于瓦片的图像 token 化
GPT-4o、GPT-4.1、GPT-4o-mini、CUA 和 o 系列(o4-mini 除外)
图像的 token 成本由两个因素决定:尺寸和细节。
任何带有 "detail": "low" 的图像都会消耗固定的基础 token 数。该数量因模型而异。要计算带有 "detail": "high", 我们执行以下操作:
- 的图像的成本,请缩放以适应 2048px x 2048px 的正方形,同时保持原始宽高比
- 缩放图像,使其最短边为 768px
- 计算图像中 512px 正方形的数量。每个正方形花费固定数量的 token,如下所示。
- 将基础 token 添加到总计中
| 模型 | 基础 token | 切片 token |
|---|---|---|
| gpt-5, gpt-5-chat-latest | 70 | 140 |
| 4o, 4.1, 4.5 | 85 | 170 |
| 4o-mini | 2833 | 5667 |
| o1, o1-pro, o3 | 75 | 150 |
| computer-use-preview | 65 | 129 |
GPT Image 1
对于 GPT Image 1,我们计算图像输入成本的方式与上述相同,不同之处在于我们会将图像缩小,使最短边为 512px 而不是 768px。价格取决于图像的尺寸以及 输入保真度.
当输入保真度设置为 low 时,基础成本为 65 个图像 token,每个切片花费 129 个图像 token。当使用高输入保真度时,除了上述图像 token 外,我们还会根据图像的宽高比添加固定数量的 token。
- 如果您的图像是正方形的,我们会添加 4160 个额外的输入图像 token。
- 如果它更接近纵向或横向,我们会添加 6240 个额外的 token。
要查看图像输入 token 的定价,请参阅我们的 中找到.
限制
虽然具有视觉能力的模型非常强大,可在多种情况下使用,但了解这些模型的局限性也很重要。以下是一些已知的局限性:
- 医学影像: 该模型不适用于解读 CT 扫描等专业医学影像,也不应用于医疗建议。
- 非英语: 在处理非拉丁字母文本(如日语或韩语)的图像时,该模型的表现可能不佳。
- 小文本: 放大图像中的文本以提高可读性。如果条件允许,请使用
"detail": "original"也能提升性能。 - 旋转: 该模型可能会误读旋转或上下颠倒的文本和图像。
- 视觉元素: 该模型可能难以理解颜色或样式(如实线、虚线或点线)发生变化的图表或文本。
- 空间推理: 该模型在处理需要精确空间定位的任务时存在困难,例如识别国际象棋棋子的位置。
- 准确性: 在某些情况下,该模型可能会生成错误的描述或说明。
- 图像形状: 该模型难以处理全景和鱼眼图像。
- 元数据和调整大小: 该模型不处理原始文件名或元数据。具体取决于图像大小和
detail级别,图像可能会在分析前被调整大小,从而影响其原始尺寸。 - 计数: 该模型对图像中物体的计数可能只是近似值。
- 验证码: 出于安全考虑,我们的系统会拦截验证码 (CAPTCHA) 的提交。
我们在 token 级别处理图像,因此我们处理的每张图像都会计入您的每分钟 token 数 (TPM) 限制。
要获取图像处理最准确和最新的估算,请使用我们提供的图像定价计算器 此处.

