withoutbg - Python SDK (Developer Guide, Focus v1.0.0)#

AI-powered background removal with local and cloud options

Latest: The new Focus v1.0.0 model is now the default for local processing. It delivers markedly better edges, hair/fur handling, and generalization than the legacy Snap model.

Install#

Installation

pip install withoutbg

Quick Start#

Local (Focus v1.0.0, free)#

Local Processing with Focus

from withoutbg import remove_background

img = remove_background("input.jpg")   # uses Focus locally
img.save("output.png")                 # RGBA with transparency

Cloud (best quality)#

Cloud API Processing

from withoutbg import remove_background

img = remove_background("input.jpg", api_key="sk_your_api_key")
img.save("output.png")

CLI#

Command Line Interface

# Local
withoutbg image.jpg

# Cloud
withoutbg image.jpg --api-key sk_your_api_key

When using the cloud API (api_key parameter), the SDK calls these endpoints:

For detailed endpoint specifications, error codes, rate limits, and advanced options, see the API Documentation.

Python API

Python API#

remove_background()#

Removes the background from a single image.

  • input_image: path-like or file-like image input.
  • api_key (optional): if provided, routes to the cloud API; otherwise uses local Focus.
  • Returns: PIL.Image.Image in RGBA (transparent background).

Example#

Remove Background Example

from withoutbg import remove_background
result = remove_background("portrait.jpg")  # Local Focus
result.save("portrait-no-bg.png")

remove_background_batch()#

Batch removal.

  • image_paths: list of input files.
  • output_dir (optional): write processed images to this directory.
  • api_key (optional): use cloud API for all.
  • Returns: list of results (e.g., saved file paths or PIL.Image.Image objects, depending on integration).

Example#

Batch Processing Example

from withoutbg import remove_background_batch

results = remove_background_batch(
    ["img1.jpg", "img2.jpg"],
    output_dir="results/"           # files written here
)

Configuration#

API key (cloud)#

Environment Variable Setup

export WITHOUTBG_API_KEY="sk_your_api_key"

Or pass per call: remove_background(..., api_key="sk_your_api_key").

Input/Output#

  • Inputs: JPG/PNG/WebP (typical).
  • Outputs: Prefer PNG/WebP to retain transparency.
  • Compositing: Use the alpha channel as mask.

Compositing Example

from PIL import Image
import withoutbg

fg = withoutbg.remove_background("subject.jpg", api_key="sk_your_api_key")  # or local
bg = Image.open("background.jpg")
bg.paste(fg, (0, 0), fg)   # alpha used as mask
bg.save("composite.png")

When to use which mode?#

ModeQualityCostRunsBest for
Focus (local)Excellent (new default)FreeYour machineScripts, privacy, offline usage
Cloud APIHighest (production)Pay-per-useManaged cloudHair/fur, thin edges, consistency

CLI Reference (short)#

CLI Usage

# Single image
withoutbg photo.jpg
withoutbg photo.jpg --output result.png
withoutbg photo.jpg --format webp --quality 90

# Cloud API
export WITHOUTBG_API_KEY="sk_your_api_key"
withoutbg photo.jpg --use-api
# or:
withoutbg photo.jpg --api-key sk_your_api_key

# Batch
withoutbg photos/ --batch --output-dir results/
withoutbg photos/ --batch --use-api --output-dir results/

Usage Analytics (Cloud)#

Usage Analytics

from withoutbg.api import StudioAPI

api = StudioAPI(api_key="sk_your_api_key")
usage = api.get_usage()  # Calls GET /available-credit
print(f"Credits: {usage['credit']}, Expires: {usage['expiration_date']}")

For detailed credit management, see: Credits API Documentation

Advanced: Direct API Access#

The SDK uses the cloud API under the hood. For advanced use cases like:

  • Custom request/response handling
  • Different programming languages
  • Serverless environments
  • Direct endpoint integration

See the API Documentation for:

The SDK is a convenience wrapper; all functionality is available through the REST API.

Models (Hugging Face)#

Focus uses a 4-stage pipeline (Depth → ISNet → Matting → Refiner) and typically yields 30–40% better edge/detail fidelity than the previous generation.

Commercial & Docs#

Support#