使用 Bria 编辑图像
通过 CometAPI 使用 Bria 图像编辑 API,可通过 POST /bria/image/edit/ 执行 erase、gen_fill、expand、enhance、upscale 或替换背景。
curl https://api.cometapi.com/bria/image/edit/replace_background \
-H "Authorization: Bearer $COMETAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"image": "https://your-image-host/source.jpg",
"prompt": "in a cozy living room"
}'
import os
import requests
# Replace replace_background with the action you need: erase, gen_fill,
# increase_resolution, remove_background, ...
response = requests.post(
"https://api.cometapi.com/bria/image/edit/replace_background",
headers={"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]},
json={
"image": "https://your-image-host/source.jpg",
"prompt": "in a cozy living room",
},
)
task = response.json()
print(task["request_id"]) # poll GET /bria/{request_id} until status COMPLETED
// Replace replace_background with the action you need: erase, gen_fill,
// increase_resolution, remove_background, ...
const response = await fetch("https://api.cometapi.com/bria/image/edit/replace_background", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
image: "https://your-image-host/source.jpg",
prompt: "in a cozy living room",
}),
});
const task = await response.json();
console.log(task.request_id); // poll GET /bria/{request_id} until status COMPLETED
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.cometapi.com/bria/image/edit/{action}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'image' => 'https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/original_image.png',
'mask' => 'https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/mask_image.png'
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.cometapi.com/bria/image/edit/{action}"
payload := strings.NewReader("{\n \"image\": \"https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/original_image.png\",\n \"mask\": \"https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/mask_image.png\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.cometapi.com/bria/image/edit/{action}")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"image\": \"https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/original_image.png\",\n \"mask\": \"https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/mask_image.png\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.cometapi.com/bria/image/edit/{action}")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"image\": \"https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/original_image.png\",\n \"mask\": \"https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/mask_image.png\"\n}"
response = http.request(request)
puts response.read_body{
"request_id": "<request_id>"
}概述
Bria 的图像编辑 API 提供了一套全面的工具,用于处理和增强图像。request_id 通过 查询状态 端点查询结果。此接口中的 sync 参数是固定的——你无需指定它。支持的操作
| Operation | Description | Documentation |
|---|---|---|
erase | 从图像中移除对象 | Bria Erase Docs |
gen_fill | 对蒙版区域进行生成式填充 | Bria Gen Fill Docs |
expand | 扩展图像画布 | Bria Expand Docs |
enhance | 增强图像质量 | Bria Enhance Docs |
increase_resolution | 提升图像分辨率 | Bria Upscale Docs |
replace_background | 替换图像背景 | Bria Background Docs |
授权
Bearer token authentication. Use your CometAPI key.
请求头
Must be application/json.
路径参数
Editing action to perform. Supported values: erase, gen_fill, expand, enhance, increase_resolution, replace_background.
请求体
Source image as a public URL or base64-encoded data URI. Accepted formats: JPEG, PNG, WebP. Maximum 12 MB.
Mask image as a public URL or base64. White areas mark the region to edit; black areas are preserved. Required for erase, gen_fill, and expand actions.
Text description of the desired edit. Required for gen_fill and replace_background actions.
Number of result variants to generate. Default: 1.
When true, the response blocks until results are ready. When false (default), returns immediately with placeholder URLs that can be polled.
响应
Success
The response is of type object.
curl https://api.cometapi.com/bria/image/edit/replace_background \
-H "Authorization: Bearer $COMETAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"image": "https://your-image-host/source.jpg",
"prompt": "in a cozy living room"
}'
import os
import requests
# Replace replace_background with the action you need: erase, gen_fill,
# increase_resolution, remove_background, ...
response = requests.post(
"https://api.cometapi.com/bria/image/edit/replace_background",
headers={"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]},
json={
"image": "https://your-image-host/source.jpg",
"prompt": "in a cozy living room",
},
)
task = response.json()
print(task["request_id"]) # poll GET /bria/{request_id} until status COMPLETED
// Replace replace_background with the action you need: erase, gen_fill,
// increase_resolution, remove_background, ...
const response = await fetch("https://api.cometapi.com/bria/image/edit/replace_background", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
image: "https://your-image-host/source.jpg",
prompt: "in a cozy living room",
}),
});
const task = await response.json();
console.log(task.request_id); // poll GET /bria/{request_id} until status COMPLETED
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.cometapi.com/bria/image/edit/{action}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'image' => 'https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/original_image.png',
'mask' => 'https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/mask_image.png'
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.cometapi.com/bria/image/edit/{action}"
payload := strings.NewReader("{\n \"image\": \"https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/original_image.png\",\n \"mask\": \"https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/mask_image.png\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.cometapi.com/bria/image/edit/{action}")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"image\": \"https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/original_image.png\",\n \"mask\": \"https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/mask_image.png\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.cometapi.com/bria/image/edit/{action}")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"image\": \"https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/original_image.png\",\n \"mask\": \"https://raw.githubusercontent.com/cometapi-dev/.github/refs/heads/main/assets/img/mask_image.png\"\n}"
response = http.request(request)
puts response.read_body{
"request_id": "<request_id>"
}