Skip to main content
The official Python SDK for Poof provides a small, typed, synchronous client built on httpx.

Installation

Requirements: Python 3.9+
The package on PyPI is poofbg, but the import name is poof.

Quick Start

Authentication

api_key is required. The SDK does not read an environment variable for you, so load it yourself:
The client holds an HTTP connection pool. Use it as a context manager, or call client.close() when you are done:

Usage Examples

From File Path

pathlib.Path objects work too.

From Bytes

From a File-like Object

From URL

Download the image first, then pass the bytes:

With Options

channels="alpha", padding, aspect-ratio crop, the matte metadata fields and the width / height / fit output-size options need poofbg 0.2.0 or later; video needs 0.3.0 or later.
Every successful image call costs 1 credit regardless of options. Failed calls are not billed. Video costs 5 credits per started second of source video (10 above 30 fps), charged when the job is accepted and refunded if it fails. Animated GIF input is billed at 5 credits per second whatever its frame rate. See Video.

Result Object

remove_background() returns a RemoveBackgroundResult:

Flagging Results for Review

matte_confidence (0–1) is how decisive the alpha mask is; matte_ambiguous_ratio (0–1) is the fraction of pixels with alpha between 0.1 and 0.9. Both are heuristics, not calibrated probabilities. The dashboard playground flags a result for review when the ambiguous ratio is above 0.15:

Account Info

client.me() returns the /me response as a plain dict. It is free and not rate limited.

Parallel Processing

The client is synchronous. To process several images at once, use a thread pool:

Error Handling

All exceptions inherit from PoofError and are chosen by HTTP status: Every exception exposes .message, .code, .details, .request_id and .status_code.

Retry with Backoff

Configuration

Type Hints

The SDK ships type hints and a py.typed marker, so mypy and IDEs understand Poof, RemoveBackgroundResult and the exception classes:

Video

remove_background() accepts video too (poofbg 0.3.0 or later). Poof detects the media type from the bytes: an image returns a RemoveBackgroundResult as above, a video is accepted as an asynchronous job and the call returns a Job instead. Wait for the job, then download its result_url.
An animated GIF works as input too, and format="gif" returns a looping transparent GIF (1-bit transparency, no audio, capped at 1280 px on the longest side, 720p for widescreen, and 50 fps) for stickers and chat. Pass wait=True to block inside the call instead:
Large files (up to 500 MB) can be fetched by Poof from a public https URL instead of uploading them:
A job that has already been submitted can be looked up by id at any time:

Video options

crop, padding, width, height and fit are image-only; sending one with a video raises ValidationError.

Job fields

wait_for_job(job, *, poll_interval=3.0, timeout=600.0, on_progress=None) polls GET /jobs/{id} (free, not rate limited) and raises PoofError with code="job_failed" if the job fails, or code="timeout" after timeout seconds. The full job lifecycle is described in the Jobs reference.