Working with AI?
Python SDK#
The withoutbg Python package provides a simple and powerful interface for AI-powered background removal. This SDK supports both local processing (free) and cloud API processing (premium quality).
Installation#
Installation Command
pip install withoutbg
Quick Start#
Quick Start Example
from withoutbg import remove_background
# Uses Snap model by default (local processing)
result = remove_background("input.jpg")
result.save("output.png")
# Use Studio API for best quality
result = remove_background("input.jpg", api_key="sk-your-api-key")
result.save("output.png")
API Reference
API Reference#
Remove Background#
remove_background(input_image, api_key=None)
Removes the background from a single image.
Parameters:#
input_image
(str): Path to the input image fileapi_key
(str, optional): API key for cloud processing. Uses Snap model by default
Returns:#
- Processed image object that can be saved or manipulated
Example:#
Remove Background Usage
from withoutbg import remove_background
# Uses Snap model by default
result = remove_background("photo.jpg")
result.save("photo_no_bg.png")
# Use Studio API for best quality
result = remove_background("photo.jpg", api_key="sk-your-api-key")
result.save("photo_studio_quality.png")
Batch Background Removal#
remove_background_batch(image_paths, output_dir=None, api_key=None)
Processes multiple images in batch for efficient background removal.
Parameters:#
image_paths
(list): List of paths to input image filesoutput_dir
(str, optional): Directory to save processed imagesapi_key
(str, optional): API key for cloud processing. Uses Snap model by default
Returns:#
- List of processed image results
Example:#
Batch Processing Example
from withoutbg import remove_background_batch
# Uses Snap model by default
image_list = ["img1.jpg", "img2.jpg", "img3.jpg"]
results = remove_background_batch(
image_list,
output_dir="processed_images/"
)
Processing Models#
Snap Model (Local/Free)#
- Quality: Good
- Cost: Free
- Processing: Local on your machine
- License: Apache 2.0
- Usage: Default (used when no API key is provided)
Studio API (Cloud/Premium)#
- Quality: Best available
- Cost: Pay-per-use
- Processing: Cloud-based AI
- Usage: Requires API key
Configuration#
Environment Variables#
Set your API key as an environment variable:
Environment Variable Setup
export WITHOUTBG_API_KEY="sk_your_api_key_here"
In Code Configuration#
API Key Configuration
import os
from withoutbg import remove_background
# Using environment variable
os.environ['WITHOUTBG_API_KEY'] = "sk_your_api_key_here"
result = remove_background("input.jpg")
# Direct parameter
result = remove_background("input.jpg", api_key="sk_your_api_key_here")
Error Handling#
Error Handling Example
from withoutbg import remove_background
import os
try:
# Check if file exists
if not os.path.exists("input.jpg"):
raise FileNotFoundError("Input image not found")
result = remove_background("input.jpg", api_key="your_key")
result.save("output.png")
except FileNotFoundError as e:
print(f"File error: {e}")
except Exception as e:
print(f"Processing error: {e}")
Advanced Usage
Advanced Usage#
Working with Different Image Formats#
Multiple Image Formats
from withoutbg import remove_background
# Supported input formats: JPG, JPEG, PNG, WEBP
input_formats = ["photo.jpg", "image.png", "picture.webp"]
for image_path in input_formats:
result = remove_background(image_path)
# Save as PNG for transparency support
output_path = image_path.replace(image_path.split('.')[-1], 'png')
result.save(output_path)
Batch Processing with Progress Tracking#
Batch Directory Processing
from withoutbg import remove_background_batch
import os
def process_directory(input_dir, output_dir, api_key=None):
# Get all image files from directory
image_extensions = ['.jpg', '.jpeg', '.png', '.webp']
image_paths = []
for filename in os.listdir(input_dir):
if any(filename.lower().endswith(ext) for ext in image_extensions):
image_paths.append(os.path.join(input_dir, filename))
print(f"Processing {len(image_paths)} images...")
# Create output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)
# Process in batch
results = remove_background_batch(
image_paths,
output_dir=output_dir,
api_key=api_key
)
print(f"Successfully processed {len(results)} images")
return results
# Usage
results = process_directory("./input_photos", "./output_photos")
Performance Tips#
- Use batch processing for multiple images to improve efficiency
- Local processing is faster for smaller images and when privacy is important
- Cloud processing provides better quality for complex images
- Optimize image size before processing for faster results
Support#
For issues with the Python SDK:
- Check that your input images are in supported formats (JPG, PNG, WEBP)
- Verify your API key is valid for cloud processing
- Ensure sufficient disk space for output images
- Report bugs and request features on the GitHub repository