Files
novafarma/scripts/BACKGROUND_REMOVAL_README.md
David Kotnik d0043ec390 Advanced background removal tool with auto-detection and edge preservation
- Auto-detects white/black/colored backgrounds
- Preserves cartoon outlines using gradient edge detection
- Aggressive dark gray removal (tolerance=60 for black bg)
- Smooth feathering to avoid jagged edges
- Test files show clean removal without outline damage
2025-12-31 00:13:16 +01:00

1.9 KiB

🧹 Background Removal Tool

Advanced background removal for game assets with auto-detection and edge preservation.

Features

Auto-detects background type - White, Black, or Colored
Preserves cartoon outlines - Uses edge detection to keep bold strokes
Smooth edges - Applies subtle feathering to avoid jagged edges
Cleans artifacts - Removes semi-transparent pixels and gray spots

Usage

Process single file:

python3 scripts/remove_bg_advanced.py path/to/image.png

Process entire directory:

python3 scripts/remove_bg_advanced.py assets/images/environment/

Dry run (see what would be done):

python3 scripts/remove_bg_advanced.py assets/images/ --dry-run

Non-recursive (only top-level files):

python3 scripts/remove_bg_advanced.py assets/images/npcs/ --no-recursive

How It Works

  1. Detection: Samples corner pixels to determine background color
  2. Edge Preservation: Uses gradient detection to identify and preserve outlines
  3. Removal: Creates alpha mask based on detected background
  4. Cleanup: Applies feathering and removes semi-transparent artifacts

Settings

  • White backgrounds: tolerance=30 (removes light gray too)
  • Black backgrounds: tolerance=60 (aggressive - removes dark gray)
  • Colored backgrounds: tolerance=30 (samples from corners)
  • Edge threshold: magnitude > 30 (lower = more edges preserved)
  • Feathering: sigma=0.5 (smooth but not blurry)

Examples

Before (white background):

  • Most, fence, building sprites

Before (black background):

  • Boss demons, dark effects, night scenes

After: Clean transparent PNG with preserved cartoon outlines!

Test Results

Bridge (white bg) - Perfect removal
Demon boss (black bg) - Clean removal without outline damage
Automatic detection works on all asset types


Created for: DolinaSmrti / NovaFarma Asset Pipeline