A simple Python script to test Photoroom's Background Removal API for processing images in bulk.
- π Process multiple images from a folder
- π¨ Remove backgrounds with transparent or custom colors
- π Multiple output sizes (preset or custom dimensions)
- πΌοΈ Multiple output formats (png, jpg, webp)
- βοΈ Optional cropping to cutout border
- π¬ Green screen despill support (v1)
- π Custom dimensions support with v2 API (Plus Plan)
- π§ Max width/height constraints with aspect ratio preservation
- π§ͺ Sandbox mode for testing
-
Install dependencies:
pip install -r requirements.txt
-
Get your API key:
- Visit Photoroom API Dashboard
- Copy your API key
-
Create input/output folders:
mkdir input_images output_images
-
Configure the script:
- Open
photoroom_processor.py - Set your
API_KEY - Configure other options as needed
- Open
- Place your images in the
input_imagesfolder - Run the script:
python photoroom_processor.py
- Find processed images in the
output_imagesfolder
Edit these variables in photoroom_processor.py:
# Your Photoroom API key
API_KEY = "your_api_key_here"
# Enable sandbox mode (adds 'sandbox_' prefix to token)
SANDBOX_MODE = True
# API Version - v2 supports custom dimensions (Plus Plan required)
USE_V2 = False # Set to True for v2/edit endpoint with custom dimensions
# Input and output folders
INPUT_FOLDER = "input_images"
OUTPUT_FOLDER = "output_images"
# Processing options
BG_COLOR = None # None for transparent, or "#FFFFFF" for white, "red", etc.
SIZE = "full" # (v1 only) Options: preview, medium, hd, full
FORMAT = "png" # Options: png, jpg, webp
CROP = False # Crop to cutout border
DESPILL = False # Remove green screen reflections (v1 only)
# Custom dimensions (v2 only - requires Plus Plan)
OUTPUT_SIZE = None # Examples: "1024x768", "800x600", "originalImage", "croppedSubject"
MAX_WIDTH = None # Maximum width in pixels (maintains aspect ratio)
MAX_HEIGHT = None # Maximum height in pixels (maintains aspect ratio)When SANDBOX_MODE = True, the script automatically adds the sandbox_ prefix to your API token. This is useful for testing without consuming your API credits.
The script supports two API endpoints:
-
v1/segment (Basic Plan) -
USE_V2 = False(default)- Preset sizes only (preview, medium, hd, full)
- Green screen despill support
- Lower cost
-
v2/edit (Plus Plan) -
USE_V2 = True- Custom dimensions support
- Max width/height constraints
- More advanced features
- Requires Plus Plan subscription
None- Transparent background (default)"#FFFFFF"- Hex color (white)"red","blue", etc. - HTML color names
preview- 0.25 Megapixelsmedium- 1.5 Megapixelshd- 4 Megapixelsfull- 36 Megapixels (default)
"1024x768"- Fixed dimensions (width x height)"800x600"- Any custom size in pixels"originalImage"- Keep original image dimensions"croppedSubject"- Crop to subject size"auto"- Automatic (default)
- Set
MAX_WIDTHto constrain width (e.g.,1920) - Set
MAX_HEIGHTto constrain height (e.g.,1080) - Both can be used together to fit within a bounding box
- Automatically maintains aspect ratio
- Useful for responsive designs
png- PNG format (default, supports transparency)jpg- JPEG formatwebp- WebP format
False- Keep original dimensions (default)True- Crop to cutout border, remove transparent pixels
False- No despill (default)True- Remove colored reflections from green backgrounds
USE_V2 = False
BG_COLOR = None # Transparent
SIZE = "full"USE_V2 = True
BG_COLOR = "#FFFFFF"
OUTPUT_SIZE = "1024x768"USE_V2 = True
OUTPUT_SIZE = "originalImage"
MAX_WIDTH = 1920USE_V2 = True
OUTPUT_SIZE = "originalImage"
MAX_WIDTH = 1920
MAX_HEIGHT = 1080Check the Photoroom pricing page for current API pricing and plans.
- JPEG (.jpg, .jpeg)
- PNG (.png)
- WebP (.webp)
Maximum image size: 30MB
==================================================
PHOTOROOM API - BACKGROUND REMOVAL POC
==================================================
API Version: v1/segment (Basic Plan)
Sandbox Mode: ON
Input Folder: input_images
Output Folder: output_images
Background Color: Transparent
Output Size: full
Output Format: png
Crop: False
Despill: False
==================================================
Found 3 images to process
==================================================
Processing: photo1.jpg...
β Saved to: photo1_processed.png
Processing: photo2.png...
β Saved to: photo2_processed.png
Processing: photo3.jpg...
β Saved to: photo3_processed.png
==================================================
Processing complete: 3/3 successful
- Check your API plan and remaining credits
- Visit your API Dashboard
- Verify your API key is correct
- Check if sandbox mode is properly configured
- Check image format and size (max 30MB)
- Verify parameter values are valid
This is a proof of concept script. Check Photoroom's terms of service for API usage rights.