Skip to main content
Version: 1.11.0-beta

Remove Background

Removes the background from an image. This method is available under client.images.removeBackground and has two variants: .json and .formData.

The endpoint for this operation is /api/v1/edits/remove-bg.

JSON Method

client.images.removeBackground.json(payload: RemoveBackgroundJsonPayload): Promise<Blob | Buffer>

Accepts a JSON payload to specify the image via URL or a previous run_id.

Parameters

  • payload (RemoveBackgroundJsonPayload): An object containing either:
    • imageUrl (string, optional): URL of the image to process.
    • run_id (string, optional): ID of a previous run whose output image should be used.

One of imageUrl or run_id must be provided.

Returns

  • Browser: Promise<Blob> - A promise that resolves to a Blob containing the image with the background removed.
  • Server: Promise<Buffer> - A promise that resolves to a Buffer containing the image with the background removed.

Example (Browser)

import { ImagineoAIClient } from "@imagineoai/javascript-sdk/browser";

const client = new ImagineoAIClient("YOUR_API_URL", { apiKey: "YOUR_API_KEY" });

async function removeBgFromUrl() {
try {
const imageBlob = await client.images.removeBackground.json({
imageUrl: "https://example.com/image.jpg",
});
// Handle the resulting imageBlob (e.g., display it)
console.log("Background removed, image blob:", imageBlob);
} catch (error) {
console.error("Error removing background:", error);
}
}

Example (Server)

import { ImagineoAIClient } from "@imagineoai/javascript-sdk/server";
import fs from 'fs/promises';

const client = new ImagineoAIClient("YOUR_API_URL", { apiKey: "YOUR_API_KEY" });

async function removeBgFromServerUrl() {
try {
const imageBuffer = await client.images.removeBackground.json({
imageUrl: "https://example.com/image.jpg",
});
await fs.writeFile("output_no_bg.png", imageBuffer);
console.log("Background removed, image saved as output_no_bg.png");
} catch (error) {
console.error("Error removing background:", error);
}
}

FormData Method

client.images.removeBackground.formData(payload: { image: File | Buffer; run_id?: string; filename?: string }): Promise<Blob | Buffer>

Accepts FormData, typically used for uploading an image file directly.

Parameters

  • payload (object):
    • image (File (Browser) | Buffer (Server)): The image file or buffer.
    • run_id (string, optional): ID of a previous run to associate with this operation.
    • filename (string, optional): Optional filename for the image when sending form-data (primarily for server-side usage with Buffers).

Returns

  • Browser: Promise<Blob> - A promise that resolves to a Blob containing the image with the background removed.
  • Server: Promise<Buffer> - A promise that resolves to a Buffer containing the image with the background removed.

Example (Browser - File Upload)

import { ImagineoAIClient } from "@imagineoai/javascript-sdk/browser";

const client = new ImagineoAIClient("YOUR_API_URL", { apiKey: "YOUR_API_KEY" });

const fileInput = document.getElementById('imageUpload'); // <input type="file" id="imageUpload">

fileInput.addEventListener('change', async (event) => {
const file = event.target.files[0];
if (file) {
try {
const imageBlob = await client.images.removeBackground.formData({ image: file });
console.log("Background removed from uploaded image:", imageBlob);
// You can create an object URL to display the image
// const objectURL = URL.createObjectURL(imageBlob);
// const imgElement = document.createElement('img');
// imgElement.src = objectURL;
// document.body.appendChild(imgElement);
} catch (error) {
console.error("Error removing background from uploaded file:", error);
}
}
});

Example (Server - Buffer)

import { ImagineoAIClient } from "@imagineoai/javascript-sdk/server";
import fs from 'fs/promises';

const client = new ImagineoAIClient("YOUR_API_URL", { apiKey: "YOUR_API_KEY" });

async function removeBgFromLocalFile() {
try {
const imageBuffer = await fs.readFile("path/to/your/image.png");
const resultBuffer = await client.images.removeBackground.formData({
image: imageBuffer,
filename: "image.png"
});
await fs.writeFile("local_image_no_bg.png", resultBuffer);
console.log("Background removed from local file, image saved.");
} catch (error) {
console.error("Error removing background from local file:", error);
}
}