withoutbg Python Library

repository·main·Indexed 22 days ago

https://github.com/withoutbg/withoutbg-python

A Python library and CLI for removing image backgrounds. It supports two modes: a free, private, and offline local mode using an open-weights ONNX model, and a high-quality Cloud API mode. The library provides tools for single image processing, batch processing, and progress tracking via callbacks, returning results as RGBA PIL Images.

Tokens
6.5K
Snippets
27
Records
38
Agent score
73%

What's inside withoutbg

  1. Compliance requirements for redistributing withoutBG Open Weights Model

    main

    The withoutBG Open Weights Model ONNX graph embeds DINOv3-derived weights and components. Users redistributing these weights must comply with the withoutBG Open Model License, which combines:

    • Apache 2.0 for withoutBG-authored portions.
    • Meta DINOv3 License for DINOv3 portions.

    When distributing the Open Weights Model or derivatives containing DINOv3 Materials, you must:

    1. Include a copy of the Meta DINOv3 License (LICENSE-DINOv3).
    2. Prominently display "Built with DINOv3" in related product documentation, app About screens, or model cards.
    3. Comply with Meta's trade-control and prohibited-use restrictions.
  2. Handle canvas size changes in v10 open-weights models

    main

    Model version 10.0.0 introduces a fixed I/O canvas size of 448×448 (previously 1024 input / 768 output).

    Integration Note: If your implementation relies on hard-coded 1024/768 letterbox sizes, you must update your logic to read the canvas_size and shapes directly from the sidecar metadata file (withoutbg-open-weights.onnx.json). If the sidecar is absent, the package defaults to 448.

  3. Understand output formats and transparency

    main

    All remove_background methods return a PIL Image in RGBA mode.

    • PNG/WebP: Preserves transparency.
    • JPEG: Transparency is dropped silently (the background will be filled, typically with black or white depending on implementation, but the alpha channel is lost).
  4. Use withoutBG Local (Open Weights) mode

    main

    Use WithoutBG.open_weights() to run the model locally on your machine. This mode is free, private, and works offline. On the first run, it will download approximately 455 MB of weights from Hugging Face.

    Note: Keep the model object alive if processing multiple images to avoid reloading weights repeatedly.

    from withoutbg import WithoutBG
    
    model = WithoutBG.open_weights()
    result = model.remove_background("input.jpg")
    result.save("output.png")
  5. Update Python API calls for v1.0.3+ (API renames)

    main

    As of version 1.0.3, several product variant names have been updated to canonical names. While the old names are deprecated aliases that still work, they should be updated to avoid DeprecationWarning and ensure compatibility with future major releases.

    Local Open Weights Model

    Replace WithoutBG.opensource() with WithoutBG.open_weights().

    Cloud API Client

    Replace ProAPI with WithoutBG.api().

    Old (deprecated)New (canonical)Notes
    WithoutBG.opensource()WithoutBG.open_weights()Emits DeprecationWarning
    OpenSourceModelOpenWeightsModelAlias, no warning yet
    ProAPIWithoutBGAPIClientAlias, no warning yet
    WithoutBGOpenSourceWithoutBGOpenWeightsAlias, no warning yet
    # Using the new canonical API for local models
    from withoutbg import WithoutBG
    model = WithoutBG.open_weights()
    result = model.remove_background("photo.jpg")
    
    # Using the new canonical API for Cloud API
    from withoutbg import WithoutBG
    api = WithoutBG.api(api_key="sk_...")
    result = api.remove_background("photo.jpg")
  6. Compliance requirements for distributing withoutBG software or models

    main

    If you distribute the withoutBG software or the Open Weights Model, you must satisfy the following attribution and licensing requirements:

    1. Include a copy of the Apache License 2.0 (LICENSE file).
    2. Include the THIRD_PARTY_LICENSES.md file.
    3. Include a copy of the Meta DINOv3 License (LICENSE-DINOv3) if distributing model weights or derivatives that embed DINOv3 Materials.
    4. Include the NOTICE file.
    5. Retain all original copyright notices.
    6. Include attribution to withoutbg.com in any public usage.
    7. Display "Built with DINOv3" when distributing the Open Weights Model (as required by the Meta DINOv3 License).
  7. Use withoutBG Cloud (API) mode

    main

    Use WithoutBG.api() to access the Cloud API, which provides higher quality results (especially for hair and fur) without requiring local GPU/CPU resources. You must provide an api_key or set the WITHOUTBG_API_KEY environment variable.

    from withoutbg import WithoutBG
    
    # Pass api_key here, or set WITHOUTBG_API_KEY in the environment
    model = WithoutBG.api(api_key="sk_your_key")
    result = model.remove_background("input.jpg")
    result.save("output.png")
  8. Migrate from the withoutbg monorepo to the standalone Python SDK

    main

    The Python SDK (pip install withoutbg) has moved from a sub-package in the withoutbg/withoutbg monorepo to its own standalone repository.

    • To use the Python package: Instead of cloning the monorepo, use pip install withoutbg or clone this repository directly.
    • To use the self-hosted Docker application: The web UI, Docker, and FastAPI components previously located in apps/web/ have moved to the withoutbg-inference repository.
  9. Configure local models using the unified ONNX graph

    main

    The model architecture has changed from a multi-file setup (depth, ISNet, matting, refiner) to a single unified ONNX graph.

    • Environment Variable: Use WITHOUTBG_MODEL_PATH to point to the unified .onnx file.
    • Deprecated Variables: The following variables are no longer used: WITHOUTBG_DEPTH_MODEL_PATH, WITHOUTBG_ISNET_MODEL_PATH, WITHOUTBG_MATTING_MODEL_PATH, and WITHOUTBG_REFINER_MODEL_PATH.
    • Metadata: A sidecar metadata file named withoutbg-open-weights.onnx.json must be located in the same directory as the .onnx file.
  10. How to choose between local Open Weights and Cloud API

    main

    The WithoutBG class provides two distinct ways to perform background removal via factory methods:

    1. Local Open Weights Model (WithoutBG.open_weights()): Runs ONNX-based models locally on your machine. It is ideal for privacy or offline use. The model is downloaded from Hugging Face on the first use if a model_path is not provided.
    2. Cloud API (WithoutBG.api()): Uses the withoutbg.com cloud service for high-quality background removal. This is ideal for high-performance needs without local compute requirements. It requires an API key.

    Both methods return an object that implements the same remove_background and remove_background_batch interface.

    from withoutbg import WithoutBG
    
    # For local execution
    model = WithoutBG.open_weights()
    
    # For cloud execution
    model = WithoutBG.api(api_key="your_api_key")
  11. Configure withoutBG via environment variables

    main

    Use the following environment variables to configure the library:

    | Variable | Description | |---|---|---| | WITHOUTBG_API_KEY | API key for Cloud mode (alternative to api_key= argument) | | WITHOUTBG_MODEL_PATH | Path to a local .onnx file (skips Hugging Face download). Note: Keep the sidecar metadata file withoutbg-open-weights.onnx.json next to the ONNX file. |