概述
当你已经有一张源图,希望 GPT Image 2 帮你改风格、替换元素、调整构图或从已有素材继续生成时,就使用图片编辑接口。
接口地址
- 方法:
POST - URL:
https://api.gptimageapi.dev/v1/images/edit
身份验证
在 Authorization 请求头中包含您的 API Key。还没有 API Key?点击这里创建。
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json请求体
{
"prompt": "把这张产品图改成更高级的棚拍效果,弱化反光,背景换成浅灰无缝纸背景。",
"image": "https://example.com/image.jpg",
"model": "gpt-image-2",
"size": "4:3",
"quality": "high",
"n": 1
}参数说明
prompt:描述你希望模型做出的修改。image:源图 URL 或 base64 data URL。model:标准编辑路线使用gpt-image-2。size:输出宽高比,例如1:1、4:3、16:9、9:21、1:2、2:1。quality:可选low、medium、high。n:返回图片数量。
积分按质量档扣除:low 每张 2 积分,medium 每张 6 积分,high 每张 12 积分。
好的编辑请求通常会包含
- 哪些部分必须保持不变。
- 哪些元素需要替换、删除或重绘。
- 目标风格、光线或材质方向。
- 如果最终图中必须出现文字,把短文本放进引号。
返回格式
{
"code": 0,
"message": "ok",
"data": {
"images": [
{
"url": "https://cdn.gptimageapi.dev/edits/1234567890.png"
}
]
}
}提示词建议
- 如果某些元素必须保留原样,直接写明“保持不变”。
- 先描述结构修改,再描述目标风格,会更稳定。
- 首轮尽量只传一张清晰源图。
- 如果图中必须出现准确文案,把短文本放进引号。
调用示例
curl
curl -X POST "https://api.gptimageapi.dev/v1/images/edit" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"prompt": "把这张产品图改成更高级的棚拍效果,弱化反光,背景换成浅灰无缝纸背景。",
"image": "https://example.com/image.jpg",
"model": "gpt-image-2",
"size": "4:3",
"quality": "high",
"n": 1
}'Node.js
async function editImage() {
const res = await fetch('https://api.gptimageapi.dev/v1/images/edit', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt:
'把这张产品图改成更高级的棚拍效果,弱化反光,背景换成浅灰无缝纸背景。',
image: 'https://example.com/image.jpg',
model: 'gpt-image-2',
size: '4:3',
quality: 'high',
n: 1,
}),
});
const result = await res.json();
if (result.code !== 0) throw new Error(result.message);
return result.data.images[0].url;
}Python
import requests
def edit_image():
url = 'https://api.gptimageapi.dev/v1/images/edit'
headers = {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
}
payload = {
'prompt': '把这张产品图改成更高级的棚拍效果,弱化反光,背景换成浅灰无缝纸背景。',
'image': 'https://example.com/image.jpg',
'model': 'gpt-image-2',
'size': '4:3',
'quality': 'high',
'n': 1,
}
res = requests.post(url, headers=headers, json=payload, timeout=60)
res.raise_for_status()
result = res.json()
if result.get('code') != 0:
raise Exception(result.get('message'))
return result.get('data', {}).get('images', [])[0].get('url')
print(edit_image())