withoutbg: Free Local & API-Based AI Background Removal Tool

9月16日 Published inImage Tools

withoutbg is a specialized AI tool designed for precise image background removal. It offers the flexibility of running tasks locally for free or utilizing a cloud API for superior results. Supporting both single images and batch processing, the tool is accessible via a Python API or a Command Line Interface (CLI). withoutbg is engineered for sharp edge detection and preserves intricate details such as hair and fur, producing clean, transparent backgrounds ideal for e-commerce product photography and automated social media content creation.

  • Two Processing Modes:

    • Local (Free): Suitable for standard daily requirements.
    • Cloud API (Paid): Delivers higher precision, making it the preferred choice for professional product photo retouching.
  • Advanced Detail Preservation: Features accurate edge detection and restores fine details effectively. The output is a high-quality image with a transparent background.

Install withoutbg

Install the package using pip. Open your terminal and execute:

pip install withoutbg

Basic Usage Examples

  • Local Single Image: withoutbg image.jpg – This command strips the background from image.jpg.

  • Cloud API (Higher Quality): First, obtain an API key from the provider. Then run: withoutbg image.jpg --api-key sk_your_api_key (ensure you replace sk_your_api_key with your actual credentials).

Python API

Background removal requires only a few lines of Python code, whether you are using local processing or the cloud service. Batch operations are also natively supported.

  • Local (Snap model, free):
from withoutbg import remove_background
result = remove_background("input.jpg")
result.save("output.png")
  • Cloud (High quality):
from withoutbg import remove_background
result = remove_background("input.jpg", api_key="sk_your_key")
  • Batch Processing:
from withoutbg import remove_background_batch
results = remove_background_batch(["img1.jpg", "img2.jpg"],
                                  output_dir="results/")

CLI Usage

The command line interface offers extensive control over output paths, file formats, quality settings, and cloud API integration.

  • Core Commands: Process a single image and define a specific output path: withoutbg photo.jpg --output result.png

    Define a custom file format and compression quality: withoutbg photo.jpg --format webp --quality 90

  • Cloud API via CLI: Configure your API key as an environment variable: export WITHOUTBG_API_KEY="sk_your_api_key" Then execute: withoutbg photo.jpg --use-api

    Alternatively, pass the key directly within the command: withoutbg photo.jpg --api-key sk_your_key

  • Batch Processing: Process every image within a specific folder: withoutbg photos/ --batch --output-dir results/

    Execute batch processing using the cloud API: withoutbg photos/ --batch --use-api --output-dir results/

Installation for Development

To contribute to the project or customize the tool, clone the repository and install it in editable mode:

  1. git clone https://github.com/withoutbg/withoutbg.git
  2. cd withoutbg
  3. pip install -e ".[dev]"

Basic Background Removal

For a standard implementation where you simply need to remove the background:

import withoutbg
output = withoutbg.remove_background("portrait.jpg")
output.save("portrait-withoutbg.png")

E-commerce Product Photo Processing

For online retailers managing high volumes of inventory, batch processing significantly reduces manual work:

import withoutbg
from pathlib import Path

product_images = Path("products").glob("*.jpg")
results = withoutbg.remove_background_batch(
    list(product_images),
    output_dir="catalog-withoutbg/",
    api_key="sk_your_key"  # Cloud API recommended for professional quality
)

Social Media Content Creation

Extract subjects from their original backgrounds and composite them onto custom backdrops:

import withoutbg
from PIL import Image

foreground = withoutbg.remove_background("selfie.jpg", api_key="sk_key")
background = Image.open("gradient_bg.jpg")
background.paste(foreground, (0, 0), foreground)
background.save("social_post.jpg")

Setting Your API Key

To access the cloud API, register for a key at withoutbg.com. You can configure it using either of the following methods:

  • Environment Variable: export WITHOUTBG_API_KEY="sk_your_api_key"
  • Direct Assignment: result = withoutbg.remove_background("image.jpg", api_key="sk_your_key")

Local Development Setup

  1. git clone https://github.com/withoutbg/withoutbg.git
  2. cd withoutbg
  3. pip install -e ".[dev]"
  4. Execute tests: pytest
  5. Format code: black src/ tests/ and ruff check src/ tests/
  6. Perform type checking: mypy src/

Usage Analytics

Monitor your API consumption through the StudioAPI:

from withoutbg.api import StudioAPI
api = StudioAPI(api_key="sk_your_key")
usage = api.get_usage()
print(usage)

Commercial API

  • Premium processing quality
  • 99.9% uptime SLA
  • Scalable infrastructure designed for high-volume enterprise needs