home / skills / michaelboeding / skills / background-remove

background-remove skill

/skills/background-remove

This skill removes image backgrounds using AI or built-in methods to produce PNG or WebP with transparent backgrounds.

npx playbooks add skill michaelboeding/skills --skill background-remove

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
5.7 KB
---
name: background-remove
description: >
  Remove backgrounds from images using AI. Triggers include:
  "remove background", "transparent background", "cut out", "isolate subject",
  "remove bg", "make transparent", "extract subject", "background removal"
  Creates PNG or WebP images with transparent backgrounds.
---

# Background Remove Skill

Remove backgrounds from images using AI (rembg/U2-Net) or built-in methods.

**Output:** PNG or WebP with transparent background.

## Quick Examples

| User Says | What Happens |
|-----------|--------------|
| "Remove the background from this photo" | AI removes background, outputs PNG |
| "Make this image transparent" | Removes background, preserves subject |
| "Cut out the product from this image" | Isolates subject with clean edges |
| "Remove backgrounds from all images in /photos" | Batch processes multiple images |
| "Quick background removal, white background" | Uses fast built-in method |

## Prerequisites

- `rembg` - AI-based background removal (recommended)
  ```bash
  pip install rembg
  # Or with GPU acceleration (faster, requires CUDA)
  pip install rembg[gpu]
  ```

- `Pillow` - Required for image processing
  ```bash
  pip install Pillow
  ```

The first run will download the U2-Net model (~170MB) which is cached for future use.

## Methods

| Method | Description | Best For |
|--------|-------------|----------|
| **rembg** | AI-based using U2-Net model | Complex images, photos, products (default) |
| **builtin** | White-to-transparent conversion | Icons, graphics with clean white backgrounds |

## Workflow

### Step 1: Gather Requirements (REQUIRED)

Use the `AskUserQuestion` tool for each question. Ask ONE question at a time.

**Q1: Image Source**
> "Which image(s) should I remove the background from?
>
> Please provide the file path or paste the image."

*Wait for response.*

**Q2: Method (Optional)**
> "Which removal method?
>
> - **AI** (rembg) - Best quality, works on any image (default)
> - **Built-in** - Faster, best for white backgrounds"

*Wait for response. Default to AI if user doesn't specify.*

**Q3: Output Location (Optional)**
> "Where should I save the result?
>
> - Same location with `_nobg` suffix (default)
> - Custom path"

*Wait for response.*

### Step 2: Execute Background Removal

**Single image:**
```bash
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "/path/to/image.jpg" \
  -o "/path/to/output.png"
```

**Batch processing:**
```bash
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "/path/to/img1.jpg" "/path/to/img2.png" "/path/to/img3.webp" \
  -o "/path/to/output_folder"
```

**Using built-in method (faster for white backgrounds):**
```bash
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "/path/to/icon.png" \
  -m builtin
```

### Step 3: Deliver Result

1. Show the result to the user
2. Confirm the background was removed successfully
3. Offer to:
   - Process additional images
   - Try a different method if quality isn't satisfactory
   - Adjust output format (PNG vs WebP)

## Script Parameters

| Parameter | Short | Description | Default |
|-----------|-------|-------------|---------|
| `--input` | `-i` | Input image path(s) | Required |
| `--output` | `-o` | Output path or directory | Auto-generated with `_nobg` suffix |
| `--method` | `-m` | Removal method (rembg, builtin) | rembg |

## Output Formats

The output format is determined by the file extension:

| Extension | Format | Notes |
|-----------|--------|-------|
| `.png` | PNG | Best quality, larger file (default) |
| `.webp` | WebP | Good compression, modern format |

## Integration with Other Skills

This skill can be called by other skills that need background removal:

### From Python (import)
```python
import sys
sys.path.insert(0, "${SKILL_PATH}/skills/background-remove/scripts")
from background_remove import remove_background

result = remove_background("/path/to/image.png", "/path/to/output.png", method="rembg")
if result.get("success"):
    print(f"Saved to: {result['file']}")
else:
    print(f"Error: {result['error']}")
```

### From Command Line
```bash
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "/path/to/image.png" \
  -o "/path/to/output.png" \
  -m rembg
```

## Error Handling

**rembg not installed:**
```
rembg not installed. Install with: pip install rembg[gpu] (or pip install rembg for CPU-only)
```
The script will automatically fall back to the built-in method.

**Image not found:**
```
Image not found: /path/to/image.png
```

**Processing failed:**
- Try a different method
- Check if the image file is corrupted
- Ensure sufficient memory for large images

## Tips for Best Results

1. **Use rembg for photos** - AI handles complex edges (hair, fur, transparent objects)
2. **Use builtin for graphics** - Faster for icons/logos with clean white backgrounds
3. **Check edges** - If edges are rough, the AI method usually gives better results
4. **Batch process** - Process multiple images at once for efficiency
5. **GPU acceleration** - Install `rembg[gpu]` for faster processing on NVIDIA GPUs

## Examples

### Remove background from a photo
```bash
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "product_photo.jpg" \
  -o "product_transparent.png"
```

### Batch process a folder
```bash
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i photos/*.jpg \
  -o "transparent_photos/"
```

### Fast removal for icons (white background)
```bash
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "icon.png" \
  -m builtin
```

### Output as WebP (smaller file size)
```bash
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "photo.jpg" \
  -o "result.webp"
```

Overview

This skill removes backgrounds from images using AI (rembg / U2-Net) or a fast built-in method. It outputs transparent PNG or WebP files and supports single-image and batch processing. The AI option is recommended for photos and complex edges, while the built-in method is faster for clean white backgrounds.

How this skill works

You provide one or more image paths and choose a method: 'rembg' (AI-powered) or 'builtin' (white-to-transparent). The skill runs a background segmentation model (U2-Net via rembg) when available, falls back to the built-in converter if not, and writes results as .png or .webp. Command-line and importable Python interfaces let other tools call the removal function programmatically.

When to use it

  • Remove backgrounds from product photos, portraits, or images with hair and fine details.
  • Convert icons, logos, or graphics with plain white backgrounds to transparent quickly.
  • Batch-process a folder of images for e-commerce or catalog uploads.
  • Prepare assets for design, compositing, or web use with transparent backgrounds.

Best practices

  • Use rembg (install rembg or rembg[gpu]) for best quality on photos and complex subjects.
  • Use the builtin method for simple graphics or when rembg is not available to save time.
  • Select PNG for maximum quality and WebP for smaller file size when transparency is needed.
  • Batch process similar images together and preview a sample before processing large sets.
  • If edges look rough, retry with the AI method or check source image quality and resolution.

Example use cases

  • Remove background from a single product photo and save as product_transparent.png.
  • Batch convert a folder of photos to transparent backgrounds for a shopping catalog.
  • Quickly make an app icon transparent using the builtin white-to-transparent method.
  • Integrate removal into a Python pipeline: import remove_background and call programmatically.
  • Output optimized WebP images for web delivery while preserving transparent subjects.

FAQ

What dependencies are required?

Install rembg for AI removal and Pillow for image handling. For GPU acceleration use rembg[gpu].

Which output formats are supported?

Outputs follow the file extension: .png for best quality and .webp for smaller transparent images.