图像API接口
Remove-bg (背景消除)
高精度背景移除API接口,提供高精度的背景移除功能,能够智能识别主体对象并精确移除背景,生成透明背景的图片
📋 基本信息
| 属性 | 值 |
|---|---|
| 接口地址 | POST /v1/chat/completions |
| 模型名称 | remove-bg |
📝 请求参数
| 路径 | 参数 | 类型 | 必填 | 说明 | 默认 |
|---|---|---|---|---|---|
| - | model | string | ✅ | 模型名称,固定为 remove-bg | - |
| - | messages | array | ✅ | 消息数组,包含用户输入的文本和图片 | - |
messages[] | role | string | ✅ | 消息角色,固定为 user | - |
messages[] | content | array | ✅ | 内容数组,包含文本和图片元素 | - |
content[] | type | string | ✅ | 内容类型:text 或 image_url | - |
content[] | text | string | ❌ | 当 type 为 text 时的文本内容 | "a person" |
content[] | image_url | object | ❌ | 当 type 为 image_url 时的图片对象 | - |
image_url | url | string | ✅ | 图片的URL地址(当type为image_url时必填) | - |
注意:remove-bg 模型需要一个图片来移除背景。
📝 请求示例
curl https://api.xty.app/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxxx" \
-d '{
"model": "remove-bg",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "a person"},
{
"type": "image_url",
"image_url": {
"url": "https://file.xty.app/docs/msk.jpg"
}
}
]
}
]
}'import requests
import json
url = "https://api.xty.app/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer sk-xxxxx"
}
data = {
"model": "remove-bg",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "a person"},
{
"type": "image_url",
"image_url": {
"url": "https://file.xty.app/docs/msk.jpg"
}
}
]
}
]
}
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.json())from openai import OpenAI
import httpx
client = OpenAI(
base_url="https://api.xty.app/v1",
api_key="sk-xxxxx",
http_client=httpx.Client(
base_url="https://api.xty.app/v1",
follow_redirects=True,
),
)
response = client.chat.completions.create(
model="remove-bg",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "a person"},
{
"type": "image_url",
"image_url": {
"url": "https://file.xty.app/docs/msk.jpg"
}
}
]
}
]
)
print(response)fetch('https://api.xty.app/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer sk-xxxxx'
},
body: JSON.stringify({
model: "remove-bg",
messages: [
{
role: "user",
content: [
{type: "text", text: "a person"},
{
type: "image_url",
image_url: {
url: "https://file.xty.app/docs/msk.jpg"
}
}
]
}
]
})
})
.then(response => response.json())
.then(data => console.log(data));📤 响应示例
成功响应
{
"id": "xty-1704067200000",
"object": "chat.completion",
"created": 1704067200,
"model": "remove-bg",
"choices": [
{
"index": 0,
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "https://file.xty.app/docs/msk-bg.png"
}
}
],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
}
}错误响应
{
"error": {
"message": "remove-bg模型需要图片,请在请求中包含图片",
"type": "invalid_request_error",
"param": null,
"code": null
}
}🖼️ 效果展示
输入示例
输入文字:请去除这张图片的背景
输入图片:

输出结果
输出图片:

💡 使用技巧
图片质量要求
- 清晰度:使用高分辨率、清晰的图片
- 主体突出:确保主体对象与背景有明显对比
- 光线均匀:避免过暗或过亮的区域
最佳实践
- 主体选择:选择轮廓清晰的主体对象
- 背景简单:简单背景比复杂背景效果更好
- 边缘处理:对于复杂边缘(如头发、毛发),可能需要后期调整
⚠️ 注意事项
- 图片要求:必须包含清晰可识别的主体对象
- 图片格式:支持通过URL和base64提供的图片,格式包括 JPEG、PNG、WebP 等
- 处理时间:根据图片复杂度,处理时间在3-10秒之间
- 文件大小:建议每张图片不超过 10MB
- URL有效期:返回的图像URL地址有效期为1个小时,请及时保存或下载