任务:图片添加背景
简洁描述:使用图片添加背景接口为前景图生成新的背景图像。
概览
- 请求方法:
POST - 路径:
/task/pic/background - 内容类型:
multipart/form-data
认证方式
- Header:
Authorization: Bearer <token>
请求示例
表单参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| image_file | file | 否 | 源图像文件(二进制),与 image_url 二选一 |
| image_url | string | 否 | 源图像 URL,与 image_file 二选一 |
| batch_size | integer | 是 | 每次生成图片数量,默认 2,最大 4,价格按数量翻倍 |
| prompt | string | 是 | 背景提示词,支持多语言,英文效果最佳,最长 1024 字符 |
| sync | integer | 否 | 是否等待结果就绪:0=异步(默认),1=同步 |
| callback_url | string | 否 | 回调通知地址 |
curl 示例
bash
curl -X POST "https://api.gpt.ge/task/pic/background" \
-H "Authorization: Bearer sk-xxxx" \
-F "image_url=https://example.com/foreground.png" \
-F "batch_size=2" \
-F "prompt=Create a beautiful scenic background for the foreground image" \
-F "sync=1"JavaScript (fetch) 示例
javascript
const formData = new FormData();
formData.append('image_url', 'https://example.com/foreground.png');
formData.append('batch_size', '2');
formData.append('prompt', 'Create a beautiful scenic background for the foreground image');
formData.append('sync', '1');
fetch('https://api.gpt.ge/task/pic/background', {
method: 'POST',
headers: {
'Authorization': 'Bearer sk-xxxx'
},
body: formData
}).then(r => r.json()).then(console.log);Python(requests)示例
python
import requests
url = 'https://api.gpt.ge/task/pic/background'
headers = {
'Authorization': 'Bearer sk-xxxx'
}
files = {
'image_url': (None, 'https://example.com/foreground.png')
}
data = {
'batch_size': '2',
'prompt': 'Create a beautiful scenic background for the foreground image',
'sync': '1'
}
response = requests.post(url, headers=headers, files=files, data=data)
print(response.json())返回示例(200)
json
{
"status": 200,
"data": {
"completed_at": 1742670627,
"created_at": 1742670626,
"download_time": 488,
"err_info": "",
"foreground_rect": {
"x": 133,
"y": 110,
"width": 604,
"height": 952
},
"image": "https://wxtechsz.oss-cn-shenzhen.aliyuncs.com/tasks/output/segmentation/3b950563-e53f-4036-a581-5ac04d0829b3-image.png",
"image_height": 1152,
"image_width": 864,
"output_type": 2,
"processed_at": 1742670626,
"progress": 100,
"result_type": "object",
"return_type": 1,
"state": 1,
"state_detail": "Complete",
"task_id": "3b950563-e53f-4036-a581-5ac04d0829b3",
"time_elapsed": 384.54998779296875,
"type": "auto"
}
}注意:该接口使用
multipart/form-data上传图像,建议仅使用image_url或image_file中的一种方式提交源图像;prompt参数建议使用英文以获得更好背景生成效果。