Rimozione degli sfondi da app, negozi online o pipeline con una singola richiesta HTTPS.
URL di base: https://bgcut.net/api/v1 · La chiave si ottiene in Chiavi API.
Autenticazione
Ogni richiesta deve includere la chiave API nell'header X-Api-Key. Le chiavi si creano e si revocano nel proprio account; a ogni chiave si può assegnare un nome per poterle ruotare per singola applicazione.
// Never ship your API key in public front-end code – proxy through your server.
const input = document.querySelector('input[type=file]');
input.addEventListener('change', async () => {
const form = new FormData();
form.append('image_file', input.files[0]);
form.append('size', 'preview');
const res = await fetch('https://bgcut.net/api/v1/remove', { method: 'POST', headers: { 'X-Api-Key': 'bgc_YOUR_API_KEY' }, body: form });
const blob = await res.blob();
document.querySelector('img#out').src = URL.createObjectURL(blob);
});
// Java 11+ HttpClient, no external libraries
import java.net.URI;
import java.net.http.*;
import java.nio.file.*;
public class BgCut {
public static void main(String[] a) throws Exception {
String key = "bgc_YOUR_API_KEY";
String boundary = "----bgcut" + System.nanoTime();
byte[] file = Files.readAllBytes(Path.of("photo.jpg"));
String head = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"image_file\"; filename=\"photo.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";
String tail = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"size\"\r\n\r\nauto\r\n--" + boundary + "--\r\n";
byte[] body = concat(head.getBytes(), file, tail.getBytes());
HttpRequest req = HttpRequest.newBuilder(URI.create("https://bgcut.net/api/v1/remove"))
.header("X-Api-Key", key)
.header("Content-Type", "multipart/form-data; boundary=" + boundary)
.POST(HttpRequest.BodyPublishers.ofByteArray(body)).build();
HttpResponse<byte[]> res = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofByteArray());
if (res.statusCode() == 200) Files.write(Path.of("result.png"), res.body());
else System.out.println(new String(res.body()));
}
static byte[] concat(byte[]... parts) { int n = 0; for (byte[] p : parts) n += p.length; byte[] out = new byte[n]; int o = 0; for (byte[] p : parts) { System.arraycopy(p, 0, out, o, p.length); o += p.length; } return out; }
}
using System.Net.Http;
using System.Net.Http.Headers;
var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-Api-Key", "bgc_YOUR_API_KEY");
using var form = new MultipartFormDataContent();
var fileContent = new ByteArrayContent(await File.ReadAllBytesAsync("photo.jpg"));
fileContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
form.Add(fileContent, "image_file", "photo.jpg");
form.Add(new StringContent("auto"), "size");
form.Add(new StringContent("png"), "format");
var res = await client.PostAsync("https://bgcut.net/api/v1/remove", form);
if (res.IsSuccessStatusCode)
await File.WriteAllBytesAsync("result.png", await res.Content.ReadAsByteArrayAsync());
else
Console.WriteLine(await res.Content.ReadAsStringAsync());
Il file immagine (multipart/form-data). JPG, PNG, WebP, GIF, BMP, TIFF; max 25 MB / 30 MP.
image_url
string
URL pubblico dell'immagine da scaricare anziché caricarla.
image_file_b64
string
Immagine codificata in base64 (facoltativamente con prefisso data:).
size
auto | preview | full
preview = fino a 640 px (usa la quota mensile gratuita, nessun credito); full = risoluzione originale (1 credito); auto (predefinito) = full se sono disponibili crediti, altrimenti preview.
format
png | jpg | webp
Formato di output. png mantiene la trasparenza; jpg viene appiattito su bg_color (bianco per impostazione predefinita).
model
fast | hq | portrait
fast (predefinito, ~1 s), hq (qualità superiore, più lento), portrait (ottimizzato per le persone).
bg_color
string
Colore esadecimale dello sfondo, es. #ffffff. Omettere per uno sfondo trasparente.
bg_blur
number
Mantiene lo sfondo originale ma lo sfoca con questo raggio in pixel (1–200), ad es. 25 per un effetto modalità ritratto. Ha la precedenza su bg_color.
crop
boolean
Ritaglia l'output al riquadro di delimitazione del soggetto.
crop_margin
integer
Margine aggiuntivo in pixel attorno al soggetto durante il ritaglio (0–200).
roi
string
Area di interesse come frazioni x,y,w,h (0–1). L'IA analizza solo l'interno di questo rettangolo; tutto ciò che è all'esterno diventa trasparente.
feather
number
Ammorbidisce i bordi con questo raggio in pixel (0–5).
quality
integer
Qualità JPG/WebP 50–100 (predefinita 92).
response
binary | json
binary (predefinito) restituisce l'immagine; json restituisce i metadati più l'immagine in base64.
Risposte
Le richieste riuscite restituiscono HTTP 200 con i byte dell'immagine e questi header:
HTTP/1.1 200 OK
Content-Type: image/png
X-Credits-Charged: 1
X-Credits-Remaining: 149
X-Free-Remaining: 5
X-Width: 3000
X-Height: 2000
X-Image-Id: 1f3a…
X-Type: hd