Task: Add Background to Image
Short description: Use the add-background API to generate a new background for a foreground image.
Overview
- Method:
POST - Path:
/task/pic/background - Content-Type:
multipart/form-data
Authentication
- Header:
Authorization: Bearer <token>
Request Example
Form Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| image_file | file | No | Source image file (binary), mutually exclusive with image_url |
| image_url | string | No | Source image URL, mutually exclusive with image_file |
| batch_size | integer | Yes | Number of images to generate per request, default 2, maximum 4; pricing scales with quantity |
| prompt | string | Yes | Background prompt, supports multiple languages; English produces the best results; max 1024 characters |
| sync | integer | No | Whether to wait for the result: 0=async (default), 1=sync |
| callback_url | string | No | Callback URL for task completion notifications |
curl Example
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) Example
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) Example
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())Response Example (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"
}
}Note: This endpoint uses
multipart/form-datato upload the image. Submit eitherimage_urlorimage_file, not both. Use English prompts for better background generation.