Skip to main content

Python SDK for useknockout — state-of-the-art background removal API.

Project description

🥊 useknockout

State-of-the-art background removal API — Python SDK.

PyPI Python License: MIT

Website · API repo · Node SDK · React SDK · CLI

useknockout — background removal API for developers

Background removal, 40× cheaper than remove.bg. MIT licensed. Self-hostable.


Install

pip install useknockout

Quickstart

from useknockout import Knockout

client = Knockout()  # uses public beta token by default
png = client.remove("photo.jpg")

with open("out.png", "wb") as f:
    f.write(png)

That's it. Free during the public beta.

Async

import asyncio
from useknockout import AsyncKnockout

async def main():
    async with AsyncKnockout() as client:
        png = await client.remove("photo.jpg")
        open("out.png", "wb").write(png)

asyncio.run(main())

Authentication

The client falls back through three sources in order:

  1. The token= argument
  2. The KNOCKOUT_TOKEN environment variable
  3. The public beta token (free, rate-limited, in the README of the API repo)

Get your own token by emailing troy@useknockout.com (paid tier launches at $0.005/image — 40× cheaper than remove.bg).

All endpoints

from useknockout import Knockout

c = Knockout()

# Remove background
c.remove("photo.jpg")                                  # returns PNG bytes
c.remove_url("https://example.com/photo.jpg")
c.remove_batch(["a.jpg", "b.jpg", "c.jpg"])            # up to 10
c.remove_batch_url(["https://...", "https://..."])

# Replace background
c.replace_background("photo.jpg", bg_color="#000000")
c.replace_background("photo.jpg", bg_url="https://example.com/bg.jpg")

# Mask only
c.mask("photo.jpg")                                    # returns grayscale PNG

# Crop to subject
c.smart_crop("photo.jpg", padding=24)

# Effects
c.shadow("photo.jpg", shadow_blur=14, shadow_opacity=0.45)
c.sticker("photo.jpg", stroke_width=20)                # WhatsApp-style
c.outline("photo.jpg", outline_color="#000000", outline_width=4)

# Presets
c.studio_shot("photo.jpg", aspect="1:1", shadow=True)  # e-commerce
c.headshot("photo.jpg", bg_color="#FFFFFF")            # LinkedIn 4:5 portrait
c.headshot("photo.jpg", bg_blur=True, blur_radius=24)  # blurred original bg

# Marketing
c.compare("photo.jpg")                                 # before/after side-by-side

# UX helpers
c.preview("photo.jpg", max_dim=512)                    # ~80ms warm
c.estimate("remove", width=1024, height=1024)          # predict latency + cost

# Telemetry
c.health()
c.stats()                                              # public usage counter

Every method that returns an image returns raw bytes. Pipe to open(path, "wb").write(...), BytesIO, PIL.Image.open(BytesIO(...)), numpy, S3 — anywhere bytes go.

Errors

All errors inherit from KnockoutError:

from useknockout import Knockout
from useknockout.errors import (
    AuthError,           # 401 / 403
    BadRequestError,     # 400
    PayloadTooLargeError,# 413 (>25 MB)
    RateLimitError,      # 429
    ServerError,         # 5xx
    KnockoutError,       # base class / network errors
)

try:
    png = client.remove("photo.jpg")
except AuthError:
    ...  # bad token
except RateLimitError:
    ...  # back off
except KnockoutError as e:
    print(f"{e.code} ({e.status}): {e.message}")

Self-host

The API is open source and runs on Modal. Deploy your own copy:

git clone https://github.com/useknockout/api
cd api
modal deploy main.py

Then point the SDK at your deployment:

client = Knockout(token="your-token", base_url="https://your-deploy.modal.run")

License

MIT. Use it however you want.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

useknockout-0.0.2.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

useknockout-0.0.2-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file useknockout-0.0.2.tar.gz.

File metadata

  • Download URL: useknockout-0.0.2.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for useknockout-0.0.2.tar.gz
Algorithm Hash digest
SHA256 efc266d24dffb23aaf259991ba9e62682e374476889761500b28f0995e94478c
MD5 96387c4dde8cbf6a81ff7cfdc9510c8b
BLAKE2b-256 5376df8e36928ba3352f1b8e2235475c5896094a2f1b32898262dd55bdd90930

See more details on using hashes here.

File details

Details for the file useknockout-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: useknockout-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for useknockout-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9f365646b707fab87566aca614678107c7ec38fe4a3709d44fbd2a7255f8475b
MD5 a016a5fb5bfd9305d8c57f56b5b1381f
BLAKE2b-256 67b142c41b1408bf4de78528c8db3f2c64de9afd3857bb2c353b49e223e805f5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page