GeezerVison is showing github.com — Initial commit — useknockout Python SDK 0.0.1 · useknockout/python@931aba8
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Python | | 2 | +__pycache__/ | | 3 | +*.py[cod] | | 4 | +*$py.class | | 5 | +*.egg-info/ | | 6 | +*.egg | | 7 | +build/ | | 8 | +dist/ | | 9 | +.eggs/ | | 10 | +.venv/ | | 11 | +venv/ | | 12 | +env/ | | 13 | +.python-version | | 14 | + | | 15 | +# Tests / coverage | | 16 | +.pytest_cache/ | | 17 | +.coverage | | 18 | +htmlcov/ | | 19 | +.mypy_cache/ | | 20 | +.ruff_cache/ | | 21 | + | | 22 | +# IDE | | 23 | +.vscode/ | | 24 | +.idea/ | | 25 | +*.swp | | 26 | +.DS_Store | | 27 | + | | 28 | +# Demo outputs | | 29 | +out-*.png | | 30 | +out-*.jpg | | 31 | +demo-*.png | | 32 | +demo-*.jpg |
Collapse file Copy file name to clipboard +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +MIT License | | 2 | + | | 3 | +Copyright (c) 2026 useknockout | | 4 | + | | 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy | | 6 | +of this software and associated documentation files (the "Software"), to deal | | 7 | +in the Software without restriction, including without limitation the rights | | 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | | 9 | +copies of the Software, and to permit persons to whom the Software is | | 10 | +furnished to do so, subject to the following conditions: | | 11 | + | | 12 | +The above copyright notice and this permission notice shall be included in all | | 13 | +copies or substantial portions of the Software. | | 14 | + | | 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | | 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | | 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | | 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | | 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | | 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | | 21 | +SOFTWARE. |
Collapse file Copy file name to clipboard +159Lines changed: 159 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +<div align="center"> | | 2 | + | | 3 | +# 🥊 useknockout | | 4 | + | | 5 | +**State-of-the-art background removal API — Python SDK.** | | 6 | + | | 7 | +[](https://pypi.org/project/useknockout/) | | 8 | +[](https://pypi.org/project/useknockout/) | | 9 | +[](LICENSE) | | 10 | + | | 11 | +[Website](https://useknockout.com) · | | 12 | +[API repo](https://github.com/useknockout/api) · | | 13 | +[Node SDK](https://github.com/useknockout/node) · | | 14 | +[React SDK](https://github.com/useknockout/react) · | | 15 | +[CLI](https://github.com/useknockout/cli) | | 16 | + | | 17 | +<img src="https://raw.githubusercontent.com/useknockout/api/main/docs/banner.png" width="800" alt="useknockout — background removal API for developers" /> | | 18 | + | | 19 | +*Background removal, 40× cheaper than remove.bg. MIT licensed. Self-hostable.* | | 20 | + | | 21 | +</div> | | 22 | + | | 23 | +--- | | 24 | + | | 25 | +## Install | | 26 | + | | 27 | +```bash | | 28 | +pip install useknockout | | 29 | +``` | | 30 | + | | 31 | +## Quickstart | | 32 | + | | 33 | +```python | | 34 | +from useknockout import Knockout | | 35 | + | | 36 | +client = Knockout() # uses public beta token by default | | 37 | +png = client.remove("photo.jpg") | | 38 | + | | 39 | +with open("out.png", "wb") as f: | | 40 | + f.write(png) | | 41 | +``` | | 42 | + | | 43 | +That's it. Free during the public beta. | | 44 | + | | 45 | +## Async | | 46 | + | | 47 | +```python | | 48 | +import asyncio | | 49 | +from useknockout import AsyncKnockout | | 50 | + | | 51 | +async def main(): | | 52 | + async with AsyncKnockout() as client: | | 53 | + png = await client.remove("photo.jpg") | | 54 | + open("out.png", "wb").write(png) | | 55 | + | | 56 | +asyncio.run(main()) | | 57 | +``` | | 58 | + | | 59 | +## Authentication | | 60 | + | | 61 | +The client falls back through three sources in order: | | 62 | + | | 63 | +1. The `token=` argument | | 64 | +2. The `KNOCKOUT_TOKEN` environment variable | | 65 | +3. The public beta token (free, rate-limited, in the README of the API repo) | | 66 | + | | 67 | +Get your own token by emailing `[email protected]` (paid tier launches at $0.005/image — 40× cheaper than remove.bg). | | 68 | + | | 69 | +## All endpoints | | 70 | + | | 71 | +```python | | 72 | +from useknockout import Knockout | | 73 | + | | 74 | +c = Knockout() | | 75 | + | | 76 | +# Remove background | | 77 | +c.remove("photo.jpg") # returns PNG bytes | | 78 | +c.remove_url("https://example.com/photo.jpg") | | 79 | +c.remove_batch(["a.jpg", "b.jpg", "c.jpg"]) # up to 10 | | 80 | +c.remove_batch_url(["https://...", "https://..."]) | | 81 | + | | 82 | +# Replace background | | 83 | +c.replace_background("photo.jpg", bg_color="#000000") | | 84 | +c.replace_background("photo.jpg", bg_url="https://example.com/bg.jpg") | | 85 | + | | 86 | +# Mask only | | 87 | +c.mask("photo.jpg") # returns grayscale PNG | | 88 | + | | 89 | +# Crop to subject | | 90 | +c.smart_crop("photo.jpg", padding=24) | | 91 | + | | 92 | +# Effects | | 93 | +c.shadow("photo.jpg", shadow_blur=14, shadow_opacity=0.45) | | 94 | +c.sticker("photo.jpg", stroke_width=20) # WhatsApp-style | | 95 | +c.outline("photo.jpg", outline_color="#000000", outline_width=4) | | 96 | + | | 97 | +# Presets | | 98 | +c.studio_shot("photo.jpg", aspect="1:1", shadow=True) # e-commerce | | 99 | +c.headshot("photo.jpg", bg_color="#FFFFFF") # LinkedIn 4:5 portrait | | 100 | +c.headshot("photo.jpg", bg_blur=True, blur_radius=24) # blurred original bg | | 101 | + | | 102 | +# Marketing | | 103 | +c.compare("photo.jpg") # before/after side-by-side | | 104 | + | | 105 | +# UX helpers | | 106 | +c.preview("photo.jpg", max_dim=512) # ~80ms warm | | 107 | +c.estimate("remove", width=1024, height=1024) # predict latency + cost | | 108 | + | | 109 | +# Telemetry | | 110 | +c.health() | | 111 | +c.stats() # public usage counter | | 112 | +``` | | 113 | + | | 114 | +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. | | 115 | + | | 116 | +## Errors | | 117 | + | | 118 | +All errors inherit from `KnockoutError`: | | 119 | + | | 120 | +```python | | 121 | +from useknockout import Knockout | | 122 | +from useknockout.errors import ( | | 123 | + AuthError, # 401 / 403 | | 124 | + BadRequestError, # 400 | | 125 | + PayloadTooLargeError,# 413 (>25 MB) | | 126 | + RateLimitError, # 429 | | 127 | + ServerError, # 5xx | | 128 | + KnockoutError, # base class / network errors | | 129 | +) | | 130 | + | | 131 | +try: | | 132 | + png = client.remove("photo.jpg") | | 133 | +except AuthError: | | 134 | + ... # bad token | | 135 | +except RateLimitError: | | 136 | + ... # back off | | 137 | +except KnockoutError as e: | | 138 | + print(f"{e.code} ({e.status}): {e.message}") | | 139 | +``` | | 140 | + | | 141 | +## Self-host | | 142 | + | | 143 | +The API is open source and runs on Modal. Deploy your own copy: | | 144 | + | | 145 | +```bash | | 146 | +git clone https://github.com/useknockout/api | | 147 | +cd api | | 148 | +modal deploy main.py | | 149 | +``` | | 150 | + | | 151 | +Then point the SDK at your deployment: | | 152 | + | | 153 | +```python | | 154 | +client = Knockout(token="your-token", base_url="https://your-deploy.modal.run") | | 155 | +``` | | 156 | + | | 157 | +## License | | 158 | + | | 159 | +MIT. Use it however you want. |
Collapse file Copy file name to clipboard +60Lines changed: 60 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +[build-system] | | 2 | +requires = ["hatchling"] | | 3 | +build-backend = "hatchling.build" | | 4 | + | | 5 | +[project] | | 6 | +name = "useknockout" | | 7 | +version = "0.0.1" | | 8 | +description = "Python SDK for useknockout — state-of-the-art background removal API." | | 9 | +readme = "README.md" | | 10 | +license = { text = "MIT" } | | 11 | +requires-python = ">=3.9" | | 12 | +authors = [{ name = "useknockout" }] | | 13 | +keywords = [ | | 14 | + "background-removal", | | 15 | + "remove-background", | | 16 | + "birefnet", | | 17 | + "ai", | | 18 | + "image-processing", | | 19 | + "useknockout", | | 20 | +] | | 21 | +classifiers = [ | | 22 | + "Development Status :: 4 - Beta", | | 23 | + "Intended Audience :: Developers", | | 24 | + "License :: OSI Approved :: MIT License", | | 25 | + "Programming Language :: Python :: 3", | | 26 | + "Programming Language :: Python :: 3.9", | | 27 | + "Programming Language :: Python :: 3.10", | | 28 | + "Programming Language :: Python :: 3.11", | | 29 | + "Programming Language :: Python :: 3.12", | | 30 | + "Programming Language :: Python :: 3.13", | | 31 | + "Topic :: Multimedia :: Graphics", | | 32 | + "Topic :: Scientific/Engineering :: Image Processing", | | 33 | +] | | 34 | +dependencies = [ | | 35 | + "httpx>=0.27.0", | | 36 | +] | | 37 | + | | 38 | +[project.optional-dependencies] | | 39 | +dev = [ | | 40 | + "pytest>=8.0", | | 41 | + "pytest-asyncio>=0.23", | | 42 | + "ruff>=0.6.0", | | 43 | + "mypy>=1.10", | | 44 | +] | | 45 | + | | 46 | +[project.urls] | | 47 | +Homepage = "https://useknockout.com" | | 48 | +Documentation = "https://github.com/useknockout/python" | | 49 | +Repository = "https://github.com/useknockout/python" | | 50 | +Issues = "https://github.com/useknockout/python/issues" | | 51 | + | | 52 | +[tool.hatch.build.targets.wheel] | | 53 | +packages = ["src/useknockout"] | | 54 | + | | 55 | +[tool.ruff] | | 56 | +line-length = 100 | | 57 | +target-version = "py39" | | 58 | + | | 59 | +[tool.ruff.lint] | | 60 | +select = ["E", "F", "I", "W", "UP"] |
Collapse file Copy file name to clipboard +47Lines changed: 47 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +""" | | 2 | +useknockout — state-of-the-art background removal API. | | 3 | +
| | 4 | +Quickstart: | | 5 | +
| | 6 | + from useknockout import Knockout | | 7 | +
| | 8 | + client = Knockout(token="kno_public_beta_4d7e9f1a3c5b2e8d6a9f7c1b3e5d8a2f") | | 9 | + png_bytes = client.remove("photo.jpg") | | 10 | + open("out.png", "wb").write(png_bytes) | | 11 | +
| | 12 | +Async: | | 13 | +
| | 14 | + import asyncio | | 15 | + from useknockout import AsyncKnockout | | 16 | +
| | 17 | + async def main(): | | 18 | + async with AsyncKnockout(token="...") as client: | | 19 | + png = await client.remove("photo.jpg") | | 20 | + open("out.png", "wb").write(png) | | 21 | +
| | 22 | + asyncio.run(main()) | | 23 | +""" | | 24 | + | | 25 | +from useknockout._version import __version__ | | 26 | +from useknockout.client import Knockout | | 27 | +from useknockout.async_client import AsyncKnockout | | 28 | +from useknockout.errors import ( | | 29 | + KnockoutError, | | 30 | + AuthError, | | 31 | + BadRequestError, | | 32 | + PayloadTooLargeError, | | 33 | + RateLimitError, | | 34 | + ServerError, | | 35 | +) | | 36 | + | | 37 | +__all__ = [ | | 38 | + "__version__", | | 39 | + "Knockout", | | 40 | + "AsyncKnockout", | | 41 | + "KnockoutError", | | 42 | + "AuthError", | | 43 | + "BadRequestError", | | 44 | + "PayloadTooLargeError", | | 45 | + "RateLimitError", | | 46 | + "ServerError", | | 47 | +] |
Collapse file Copy file name to clipboard +73Lines changed: 73 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +"""Internal helpers shared by sync + async clients.""" | | 2 | + | | 3 | +from __future__ import annotations | | 4 | + | | 5 | +import os | | 6 | +from pathlib import Path | | 7 | +from typing import Any, Dict, List, Optional, Tuple, Union | | 8 | + | | 9 | +FileInput = Union[str, Path, bytes, bytearray] | | 10 | +"""Anything the SDK accepts as image input. | | 11 | +
| | 12 | +- ``str`` / ``Path``: filesystem path to read. | | 13 | +- ``bytes`` / ``bytearray``: raw image bytes. | | 14 | +""" | | 15 | + | | 16 | + | | 17 | +def _to_bytes_and_name(file: FileInput, default_name: str = "image") -> Tuple[bytes, str]: | | 18 | + """Normalize a FileInput to (bytes, filename) for httpx multipart.""" | | 19 | + if isinstance(file, (bytes, bytearray)): | | 20 | + return bytes(file), default_name | | 21 | + if isinstance(file, (str, Path)): | | 22 | + path = Path(file) | | 23 | + return path.read_bytes(), path.name | | 24 | + raise TypeError(f"file must be path | bytes, got {type(file).__name__}") | | 25 | + | | 26 | + | | 27 | +def _multipart_files( | | 28 | + file: FileInput, | | 29 | + extra_files: Optional[List[FileInput]] = None, | | 30 | +) -> List[Tuple[str, Tuple[str, bytes, str]]]: | | 31 | + """Build httpx-style files= argument for one or many uploads.""" | | 32 | + files: List[Tuple[str, Tuple[str, bytes, str]]] = [] | | 33 | + data, name = _to_bytes_and_name(file) | | 34 | + files.append(("file", (name, data, "application/octet-stream"))) | | 35 | + if extra_files: | | 36 | + for f in extra_files: | | 37 | + d, n = _to_bytes_and_name(f) | | 38 | + files.append(("files", (n, d, "application/octet-stream"))) | | 39 | + return files | | 40 | + | | 41 | + | | 42 | +def _multipart_batch(files_in: List[FileInput]) -> List[Tuple[str, Tuple[str, bytes, str]]]: | | 43 | + """Build files= for /remove-batch (field name ``files``, repeated).""" | | 44 | + out: List[Tuple[str, Tuple[str, bytes, str]]] = [] | | 45 | + for i, f in enumerate(files_in): | | 46 | + data, name = _to_bytes_and_name(f, default_name=f"image-{i}") | | 47 | + out.append(("files", (name, data, "application/octet-stream"))) | | 48 | + return out | | 49 | + | | 50 | + | | 51 | +def _form(data: Dict[str, Any]) -> Dict[str, str]: | | 52 | + """Drop None values, stringify the rest, for httpx data= multipart fields.""" | | 53 | + out: Dict[str, str] = {} | | 54 | + for k, v in data.items(): | | 55 | + if v is None: | | 56 | + continue | | 57 | + if isinstance(v, bool): | | 58 | + out[k] = "true" if v else "false" | | 59 | + else: | | 60 | + out[k] = str(v) | | 61 | + return out | | 62 | + | | 63 | + | | 64 | +def _resolve_token(token: Optional[str]) -> Optional[str]: | | 65 | + """Resolve token from arg or KNOCKOUT_TOKEN env var.""" | | 66 | + if token: | | 67 | + return token | | 68 | + env = os.environ.get("KNOCKOUT_TOKEN", "").strip() | | 69 | + return env or None | | 70 | + | | 71 | + | | 72 | +DEFAULT_BASE_URL = "https://useknockout--api.modal.run" | | 73 | +PUBLIC_BETA_TOKEN = "kno_public_beta_4d7e9f1a3c5b2e8d6a9f7c1b3e5d8a2f" |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +__version__ = "0.0.1" |
|