Background Removal API Endpoint

Version 2.197 (Release Notes ↗)

Description

The Background Removal API (BG-REMOVE) enables developers to programmatically remove backgrounds from images and video frames using a single API call. The endpoint intelligently detects and extracts the main subject (typically a person or object), isolating it from any distracting or unnecessary background.

Powered by advanced deep learning segmentation models, this API delivers pristine, production-ready results, eliminating the need for manual pixel selection or masking. It's perfectly suited for developers, content creators, and marketing agencies seeking automated background removal at scale, capable of processing numerous regions and images of diverse sizes. From crafting creative tools and automating e-commerce product pipelines to enhancing user-uploaded images, BG-REMOVE offers an unparalleled solution for precise, high-quality background removal at scale.

A typical use case of this API includes removing backgrounds from product images for online stores, enabling a clean and professional presentation like the following example:

Input Image to Remove Background From
Input Image to remove background from
Background Removed Image
Background Removed Image

Developer Highlights:

  • REST-API, NO SDK-Required. Cross-platform compatible (works via HTTP/S)
  • Alpha channel support for transparent PNGs
  • Fully asynchronous-friendly and embeddable in serverless functions or mobile apps
  • Supports automation in media pipelines, no UI required

The BG-REMOVE API endpoint supports single image and batch processing, varying resolutions, and complex background scenes. Output images retain foreground sharpness and transparency (alpha mask) where supported. It is the same engine that powers the PixLab Bulk Background Removal ↗ app.

To start integrating BG-REMOVE on your code base, obtain your API Key first from the PixLab Console ↗ and call the BG-REMOVE endpoint with your image input. Usage Code Samples in various programming languages, ready to be integrated are available in the section below ↓.

Looking to remove text or watermarks from images instead? Use the TXT-REMOVE API Endpoint. Want to detect or translate visible text in your image? Try the Image Text Translation API.

HTTP Methods

GET, POST

HTTP Parameters

Required

Fields Type Description
img URL Input image URL to remove the background or unwanted objects from. If you want to upload an image directly from your app, then submit a multipart/form-data POST request instead. Refer to the POST Request Data section below.
key String Your PixLab API Key ↗. You can also embed your key in the WWW-Authenticate: HTTP header and omit this parameter if you want to.

Optional

Fields Type Description
compress Boolean By default, the Background Remove API endpoint compresses JPEG output images to expedite data transfer (PNG images are lossless). Set this field to false to disable compression for JPEG images entirely.
blob Boolean By default, the Background remove API endpoint always returns a JSON Object (see Response Details ↓) containing the BASE64 encoded image output or a direct link to the output image stored in your private AWS S3 bucket if you've already connected your S3 storage credentials from the PixLab Console ↗. However, if this parameter is set to true, the raw image binary content is returned instead (see the Code Sample section for implementation details).

POST Request Body

This section details the requirements for using a POST request instead of a simple GET request.

Allowed Content-Types:

  • multipart/form-data
  • application/json

Use multipart/form-data to directly upload your image from you app (Refer to The PixLab Github Repository↗ for a working example). If you're using JSON, the media file must already be uploaded. Consider calling store to upload an image before invoking this endpoint.

HTTP Response

application/json

By default, the Background removal API endpoint always returns a JSON Object containing the BASE64 encoded image output or a direct link to the output image stored in your private AWS S3 bucket if you've already connected your S3 storage credentials from the PixLab Console ↗. if one the other side, the blob parameter (documented above) is set to true, the raw image binary content is returned instead.

Fields Type Description
status Integer HTTP 200 indicates success. Any other code indicates failure.
imgData Base64 Data Base64 encoded string of the output image data.
mimeType String Mime type such as image/png of the output image.
extension String Extension such as jpeg of the output image.
link URL Optionally, a direct link to the output image (instead of the imgData field) stored on your own AWS S3 bucket if you already connected your AWS S3 credentials from the PixLab Console ↗.
error String Error description when status != 200.
blob BLOB Optionally, the image blob data is returned instead of this JSON object if the blob parameter (documented above) is set to true.

Code Samples


import requests
import json
import base64
import os

# Programmatically remove backgrounds from input images using the PixLab BG-REMOVE API endpoint.
#
# Refer to the official documentation at: https://pixlab.io/endpoints/background-remove-api for the API reference
# guide and more code samples.

# Use POST to upload the image directly from your local folder. If your image is publicly available
# then make a simple GET request with a link to your image.
req = requests.post(
    'https://api.pixlab.io/bgremove',
    files={
        'file': open('./local_image.png', 'rb')  # The local image we are going to remove background from
    },
    data={
        'key': 'PIXLAB_API_KEY'  # PixLab API Key - Get yours from https://console.pixlab.io/
    }
)
reply = req.json()
if reply['status'] != 200:
    print(reply['error'])
else:
    imgData = reply['imgData']  # Base64 encoding of the output image
    mimetype = reply['mimeType']  # MIME type (i.e image/jpeg, etc.) of the output image
    extension = reply['extension']  # File extension (e.g., 'png', 'jpeg')

    # Decode base64 and save to disk
    try:
        img_bytes = base64.b64decode(imgData)
        output_filename = f"output_image.{extension}"
        with open(output_filename, "wb") as f:
            f.write(img_bytes)
        print(f"Background Removed Image saved to: {output_filename}")
    except Exception as e:
        print(f"Error saving output image: {e}")
← Return to API Endpoint Listing