gpt-image-2
最后更新:2026-11-14
POSThttps://www.sagelib.com/images/edits
gpt-image-2 模型的图像编辑接口,OpenAI兼容,支持图像编辑、蒙版编辑等。
接口鉴权方式请参考:API接入与鉴权
请求参数
请求头
| 参数名称 | 描述 |
|---|---|
*Authorization string |
鉴权令牌,格式为 Bearer ${AIGATEWAY_TOKEN} |
*Content-Type string |
默认值:application/json 可选值:application/json application/json(JSON 端点)或 multipart/form-data; boundary=...(multipart 端点,保持原始 boundary) |
Body 参数
| 参数名称 | 描述 |
|---|---|
*model string |
可选值:"gpt-image-2" 模型名称 |
*image string |
可选值:Image URL 待编辑原图的 URL 地址 |
*prompt string |
可选值:Any text 描述期望的图像编辑效果 |
mask string |
可选值:Mask image URL 遮罩图,Alpha 通道指定编辑区域 |
n integer |
默认值:1 可选值:1 ~ 10 生成的候选图像数量 |
size string |
默认值:auto 可选值:auto / 宽x高(宽高均需被 16 整除) 输出图像尺寸 |
quality string |
默认值:auto 可选值:low、medium、high、auto 图像质量等级 |
input_fidelity string |
可选值:low、high 与原图的保真度,low 允许更大改动,high 尽量保持原图特征 |
background string |
默认值:opaque 可选值:opaque、auto 背景类型,不支持 transparent |
output_format string |
默认值:png 可选值:png、jpeg 输出图像格式,不支持 webp |
output_compression integer |
可选值:0 ~ 100 (jpeg only) JPEG 输出压缩质量 |
moderation string |
默认值:auto 可选值:auto、low 内容审核等级 |
user string |
可选值:Any string 终端用户标识 |
请求示例
curl -v 'https://www.sagelib.com/images/edits' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"image": "${image_url}",
"prompt": "Replace the background with a starry night scene, keeping the cat's pose and fur color unchanged, realistic style, galaxy clearly visible"
}'
import json
import requests
# Configure base variables
base_url = "https://www.sagelib.com" # 把 base_url 换成你的网关地址 https://www.sagelib.com
api_key = "***" # Replace with your Bearer token
url = f"https://www.sagelib.com/images/edits"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
data = {
"model": "gpt-image-2",
"image": "${image_url}",
"prompt": "Replace the background with a starry night scene, keeping the cat's pose and fur color unchanged, realistic style, galaxy clearly visible"
}
# Send the POST request
response = requests.post(url, headers=headers, json=data)
# Print the status code and the returned JSON data
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/images/edits";
String jsonRequestBody = """
{
"model": "gpt-image-2",
"image": "${image_url}",
"prompt": "Replace the background with a starry night scene, keeping the cat's pose and fur color unchanged, realistic style, galaxy clearly visible"
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
// Use ofInputStream to handle the streaming response
HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if ("[DONE]".equals(data)) {
break;
}
// Tip: you can bring in Gson or Jackson here to parse the JSON in data and extract delta.content
System.out.println("Received stream chunk: " + data);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/images/edits"
jsonRequestBody := `
{
"model": "gpt-image-2",
"image": "${image_url}",
"prompt": "Replace the background with a starry night scene, keeping the cat's pose and fur color unchanged, realistic style, galaxy clearly visible"
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
// Pretty-print the returned JSON data
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
curl -v 'https://www.sagelib.com/images/edits' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"image": "${image_url}",
"mask": "${image_url}",
"prompt": "Replace the background with a starry night scene, keeping the cat's pose and fur color unchanged, realistic style",
"size": "1024x1024",
"quality": "high",
"input_fidelity": "high",
"n": 2,
"output_format": "jpeg",
"output_compression": 80,
"background": "opaque",
"moderation": "auto"
}'
import json
import requests
# Configure base variables
base_url = "https://www.sagelib.com" # 把 base_url 换成你的网关地址 https://www.sagelib.com
api_key = "***" # Replace with your Bearer token
url = f"https://www.sagelib.com/images/edits"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
data = {
"model": "gpt-image-2",
"image": "${image_url}",
"mask": "${image_url}",
"prompt": "Replace the background with a starry night scene, keeping the cat's pose and fur color unchanged, realistic style",
"size": "1024x1024",
"quality": "high",
"input_fidelity": "high",
"n": 2,
"output_format": "jpeg",
"output_compression": 80,
"background": "opaque",
"moderation": "auto"
}
# Send the POST request
response = requests.post(url, headers=headers, json=data)
# Print the status code and the returned JSON data
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/images/edits";
String jsonRequestBody = """
{
"model": "gpt-image-2",
"image": "${image_url}",
"mask": "${image_url}",
"prompt": "Replace the background with a starry night scene, keeping the cat's pose and fur color unchanged, realistic style",
"size": "1024x1024",
"quality": "high",
"input_fidelity": "high",
"n": 2,
"output_format": "jpeg",
"output_compression": 80,
"background": "opaque",
"moderation": "auto"
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
// Use ofInputStream to handle the streaming response
HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if ("[DONE]".equals(data)) {
break;
}
// Tip: you can bring in Gson or Jackson here to parse the JSON in data and extract delta.content
System.out.println("Received stream chunk: " + data);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/images/edits"
jsonRequestBody := `
{
"model": "gpt-image-2",
"image": "${image_url}",
"mask": "${image_url}",
"prompt": "Replace the background with a starry night scene, keeping the cat's pose and fur color unchanged, realistic style",
"size": "1024x1024",
"quality": "high",
"input_fidelity": "high",
"n": 2,
"output_format": "jpeg",
"output_compression": 80,
"background": "opaque",
"moderation": "auto"
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
// Pretty-print the returned JSON data
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
响应参数
Body 参数
| 参数名称 | 描述 |
|---|---|
created integer |
图像编辑时间戳(Unix 秒) |
model string |
实际使用的模型名称 |
data array |
生成的图像列表 |
data[0] object |
|
b64_json string |
Base64 编码的图像二进制数据 |
usage object |
Token 用量统计 |
input_tokens integer |
输入 Token 总数(含原图图像 Token) |
output_tokens integer |
输出 Token 总数 |
total_tokens integer |
总计 Token 数 |
size string |
实际输出的图像尺寸 |
quality string |
实际生效的图像质量等级 |
background string |
背景类型(始终为 opaque) |
output_format string |
实际输出的图像格式 |
POSThttps://www.sagelib.com/openai/images/edits
gpt-image-2 模型的图像编辑接口,OpenAI直连模式(URL 前缀 /openai),支持遮罩局部编辑、流式返回等。
接口鉴权方式请参考:API接入与鉴权
请求参数
请求头
| 参数名称 | 描述 |
|---|---|
*Authorization string |
鉴权令牌,格式为 Bearer ${AIGATEWAY_TOKEN} |
*Content-Type string |
可选值:multipart/form-data multipart/form-data; boundary=${boundary},boundary由请求工具自动生成 |
Body 参数
| 参数名称 | 描述 |
|---|---|
*model string |
可选值:gpt-image-2 指定用于图像编辑的模型名称 |
*prompt string |
可选值:Text 编辑指令文本,描述期望对输入图做的修改 |
*image string |
可选值:Image URL string, or multipart file (png/jpeg/webp) 待编辑的输入图像 |
mask string |
可选值:PNG file (with alpha channel) 遮罩图,指定需要重绘/保留的区域(透明区域参与编辑) |
n integer |
默认值:1 可选值:1~10 一次请求生成的图像数量 |
size string |
默认值:1024x1024 可选值:1024x1024, 1536x1024; width and height must both be divisible by 16 输出图像尺寸(宽x高) |
quality string |
默认值:auto 可选值:auto / low / high 生成质量档位,影响画面细节与 token 消耗 |
background string |
默认值:auto 可选值:auto / opaque / transparent 输出图像的背景处理方式 |
output_format string |
默认值:png 可选值:png / webp / jpeg 输出图像的编码格式 |
output_compression integer |
可选值:0-100 (jpeg/webp only) 输出图像压缩级别,值越大压缩越强、体积越小 |
response_format string |
默认值:b64_json 可选值:b64_json / url 指定图像数据在响应中的返回形式 |
user string |
调用方标识,供上游做风控与计费归因 |
input_fidelity string |
可选值:high / low 输入图像保真度,控制输出对原图的保留程度 |
stream boolean |
默认值:false 可选值:true、false 是否以流式(SSE)方式返回生成过程 |
partial_images integer |
可选值:0~3 流式生成过程中回传的中间图像数量 |
moderation string |
默认值:auto 可选值:auto / low 内容审核严格程度 |
请求示例
curl -v 'https://${base_url}/v2/llm/images/edits' \
-H "Authorization: Bearer ***" \
-F "image=${image_url}" \
-F "model=gpt-image-2" \
-F "n=1" \
-F "prompt=Replace the background with a starry night sky, keeping the cat's posture and fur color unchanged, photorealistic style, with the Milky Way clearly visible" \
-F "size=1024x1024"
curl -v 'https://${base_url}/v2/llm/images/edits' \
-H "Authorization: Bearer ***" \
-F "image=${image_url}" \
-F "mask=@${image_path}" \
-F "model=gpt-image-2" \
-F "n=1" \
-F "prompt=Replace the background with a starry night sky, keeping the cat's posture and fur color unchanged, photorealistic style, with the Milky Way clearly visible" \
-F "size=1024x1024"
curl -v 'https://${base_url}/v2/llm/images/edits' \
-H "Authorization: Bearer ***" \
-F "image=${image_url}" \
-F "model=gpt-image-2" \
-F "n=1" \
-F "output_compression=80" \
-F "output_format=jpeg" \
-F "prompt=Replace the background with a starry night sky, keeping the cat's posture and fur color unchanged, photorealistic style, with the Milky Way clearly visible" \
-F "size=1024x1024"
响应参数
Body 参数
| 参数名称 | 描述 |
|---|---|
created integer |
响应生成时间戳(Unix 秒) |
model string |
实际调用的模型名,回显为 gpt-image-2 |
data array |
生成结果数组,元素个数等于请求的 n |
b64_json string |
Base64 编码的图像数据(本端点不返回 URL 形式) |
usage object |
Token 用量统计 |
input_tokens integer |
输入总 token 数 |
input_tokens_details object |
输入 token 细分 |
image_tokens integer |
输入中的图像 token 数 |
text_tokens integer |
输入中的文本 token 数 |
output_tokens integer |
输出总 token 数 |
output_tokens_details object |
输出 token 细分 |
image_tokens integer |
输出中的图像 token 数 |
total_tokens integer |
总 token 数 |
size string |
回显实际输出尺寸 |
quality string |
回显质量档位 |
background string |
回显背景处理方式 |
output_format string |
回显输出格式 |