A powerful Python tool for automatically removing backgrounds from images and replacing them with professional backgrounds while maintaining image quality and dimensions.
Perfect for e-commerce product photography, automotive listings, or any scenario where you need consistent, professional backgrounds.
- ๐ค AI-Powered Background Removal - Uses state-of-the-art U-2-Net deep learning model
- ๐ผ๏ธ Custom Background Replacement - Replace with your own backgrounds or use default studio background
- ๐ Quality Preservation - Maintains original image dimensions and quality
- โก Batch Processing - Process multiple images at once with progress tracking
- ๐ฏ Multiple AI Models - Choose from different models optimized for various subjects
- ๐จ Flexible Output - Supports JPEG, PNG, and WEBP formats
- ๐ Detailed Logging - Track processing with colorful console output and log files
- โ๏ธ Highly Configurable - YAML-based configuration with CLI overrides
- Python 3.8 or higher
- ~200 MB disk space for AI models (downloaded automatically on first run)
- Sufficient RAM based on image sizes (recommended: 4GB+ for 4K images)
cd /path/to/ImgForgemacOS/Linux:
python3 -m venv venv
source venv/bin/activateWindows:
python -m venv venv
venv\Scripts\activatepip install --upgrade pip
pip install -r requirements.txtThe first run will automatically download the AI model (~176 MB). This is a one-time process.
ImgForge/
โโโ main.py # Main entry point
โโโ config.yaml # Configuration file
โโโ requirements.txt # Python dependencies
โโโ README.md # This file
โโโ PRD.md # Product requirements document
โโโ src/
โ โโโ __init__.py
โ โโโ background_remover.py # AI background removal
โ โโโ background_replacer.py # Background replacement logic
โ โโโ image_processor.py # Main processing pipeline
โ โโโ utils.py # Utility functions
โโโ input/ # Place your images here
โโโ output/ # Processed images go here
โโโ backgrounds/ # Background images
โ โโโ default_bg.jpeg # Default background
โโโ images/ # Example images
โ โโโ raw.jpeg # Example input
โ โโโ output.jpeg # Example output
โ โโโ sample_bg.jpeg # Sample background
โโโ logs/ # Processing logs
โโโ processing.log
# Copy your images to the input folder
cp /path/to/your/images/*.jpg input/# Copy your custom background to backgrounds folder
cp /path/to/background.jpg backgrounds/Edit config.yaml to use your background:
default_background: "./backgrounds/your_background.jpg"Ensure virtual environment is activated:
source venv/bin/activate # macOS/Linux
# OR
venv\Scripts\activate # WindowsRun with default settings:
python main.pyThat's it! Check the output/ folder for your processed images.
# Process all images in input/ folder with default settings
python main.py# Use a different config file
python main.py --config my_config.yaml
# Override input/output folders
python main.py --input ./my_photos --output ./processed
# Use a specific background image
python main.py --background ./backgrounds/studio_white.jpg
# Use a different AI model (faster but less accurate)
python main.py --model u2netp
# Set output quality
python main.py --quality 85
# Enable verbose logging
python main.py --verbosepython main.py \
--input ./car_photos \
--output ./processed_cars \
--background ./backgrounds/garage.jpg \
--quality 95 \
--verboseEdit config.yaml to customize behavior:
# Input/Output
input_folder: "./input"
output_folder: "./output"
default_background: "./backgrounds/default_bg.jpeg"
# Output Settings
output_format: "JPEG" # JPEG, PNG, or WEBP
output_quality: 95 # 1-100 (higher = better quality)
preserve_original_format: false # Keep original format
# AI Model Selection
model:
"u2net" # Options:
# - u2net: Most accurate (slower)
# - u2netp: Faster, less accurate
# - u2net_human_seg: For people
# - u2net_cloth_seg: For clothing
# Processing Options
add_shadow: false # Add shadow effect (experimental)
center_subject: true # Center subject on background
skip_existing: true # Skip already processed files
continue_on_error: true # Keep going if one file fails
# Logging
log_level: "INFO" # DEBUG, INFO, WARNING, ERROR
log_file: "./logs/processing.log"Choose the right model for your use case:
| Model | Best For | Speed | Accuracy |
|---|---|---|---|
u2net |
General purpose, vehicles, products | Slower | Highest |
u2netp |
When speed matters | Faster | Good |
u2net_human_seg |
People, portraits | Medium | High for humans |
u2net_cloth_seg |
Clothing, fashion items | Medium | High for clothing |
==================================================
ImgForge
==================================================
Input folder: ./input
Output folder: ./output
Found 3 image(s) to process
Processing: 100%|โโโโโโโโโโโโ| 3/3 [00:12<00:00, 4.2s/image]
[1/3] โ car_001.jpg (3.2s)
[2/3] โ car_002.jpg (4.5s)
[3/3] โ car_003.jpg (3.8s)
==================================================
Processing Complete!
==================================================
Total images: 3
Successful: 3
Failed: 0
Total time: 11.5s
Average time: 3.8s per image
Output saved to: ./output
Solution: Make sure virtual environment is activated:
source venv/bin/activate # macOS/Linux
# OR
venv\Scripts\activate # WindowsSolution:
- Ensure images are in the
input/folder - Check supported formats: JPEG, PNG, WEBP, BMP
- Verify file extensions are lowercase or uppercase (both work)
Solution:
- Check internet connection
- Models are downloaded from GitHub - ensure access
- Try manually downloading from: https://github.com/danielgatis/rembg
Solution:
- Resize images before processing
- Use
u2netpmodel (lighter weight) - Process fewer images at once
- Increase system RAM if possible
Solution:
- Try different models (
u2netis most accurate) - Ensure input images have clear subject/background separation
- Check that images are well-lit and in focus
- For specific subjects, use specialized models (human_seg, cloth_seg)
Edit config.yaml or modify src/utils.py::get_image_files() to support additional formats.
The tool creates a default gray gradient background if none is specified. Customize in src/image_processor.py::_create_default_background().
Enable in config:
add_shadow: trueNote: This is experimental and may need tuning for your specific images.
E-commerce Product Photography:
- Take photos of products with any background
- Place all images in
input/folder - Add your brand's background to
backgrounds/ - Configure
config.yamlwith your brand settings - Run:
python main.py - Upload processed images from
output/to your store
Automotive Listings:
The example images show a car with open boot being transformed from a garage setting to a professional studio background - perfect for dealership listings.
This is a standalone tool. Feel free to fork and customize for your needs.
This project uses open-source libraries:
rembg- Licensed under MITPillow- Licensed under HPNDOpenCV- Licensed under Apache 2.0
- Background removal powered by rembg
- U-2-Net model by Xuebin Qin et al.
For issues or questions:
- Check the troubleshooting section above
- Review
logs/processing.logfor detailed error messages - Ensure all dependencies are correctly installed
- Verify Python version (3.8+)
- Good Lighting: Well-lit images with clear subject/background separation
- High Resolution: Higher quality inputs = better outputs
- Clean Edges: Avoid motion blur or out-of-focus edges
- Consistent Backgrounds: Use the same background image for all products in a set
- Test Settings: Try different models and quality settings to find what works best
Happy Processing! ๐
When done, remember to deactivate your virtual environment:
deactivate