---
name: bg-remove
description: Remove backgrounds from images using local AI (rembg). Use when removing backgrounds from character art, mascot images, photos, or any image that needs a transparent background.
user_invocable: true
title: bg-remove
canonical_url: https://skilld.dev/gh/peterkrueck/claude-code-development-kit/bg-remove
last_updated: 2026-10-01T00:04:23.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> If the user asked to install this Skill, run `npx skilld install peterkrueck/claude-code-development-kit/bg-remove`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/bg-remove`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/peterkrueck/claude-code-development-kit/bg-remove) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/bg-remove`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/bg-remove --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/bg-remove --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Background Remove — Local AI Background Removal

Remove backgrounds from images using rembg (local, offline, no data sent externally). Outputs RGBA PNG with proper transparency.

## Input

Arguments after `/bg-remove`:
- **Source image path** (required) — path to the image
- **`--trim`** (optional) — auto-trim transparent padding after removal
- **`--output <path>`** (optional) — custom output path. Default: same directory, `<name>-transparent.png`

Examples:
- `/bg-remove assets/character/mascot.png`
- `/bg-remove image.png --trim`
- `/bg-remove image.png --output ~/Desktop/result.png`

## Setup

rembg is installed in a dedicated venv. Always activate it before use:

```bash
source ~/.claude/tools/rembg-env/bin/activate
```

If the venv doesn't exist, install it:

```bash
python3 -m venv ~/.claude/tools/rembg-env && source ~/.claude/tools/rembg-env/bin/activate && pip install "rembg[cpu,cli]"
```

Model files are cached in `~/.u2net/` (downloaded on first use per model, ~170MB for birefnet-general).

## Process

### Step 1: Verify Input

1. Check the source image exists
2. Get dimensions: `sips -g pixelWidth -g pixelHeight <path>`
3. View the image with the Read tool to understand what we're working with

### Step 2: Remove Background

Use the `birefnet-general` model — validated in testing on illustrated/character art *and* general photos, producing clean edges across both.

```bash
source ~/.claude/tools/rembg-env/bin/activate && rembg i -m birefnet-general <input> <output>
```

**Model choice:** Default to `birefnet-general`. In side-by-side testing it gave clean edges on both illustrated subjects and photographic ones. Avoid anime-trained models (e.g. `isnet-anime`): on non-anime and even some illustrated inputs they tend to add artifacts and leave dark patches around edges. If `birefnet-general` underperforms on a specific image, compare against another general model rather than an anime-specific one.

### Step 3: Verify Result

The Read tool renders transparency as black, so you MUST verify by compositing on a colored background:

```bash
source ~/.claude/tools/rembg-env/bin/activate && python3 -c "
from PIL import Image
import numpy as np

# Load result
img = Image.open('<output>').convert('RGBA')
alpha = np.array(img)[:,:,3]
total = alpha.size
transparent = np.sum(alpha == 0)
opaque = np.sum(alpha == 255)
print(f'Dimensions: {img.size}')
print(f'Transparent: {transparent/total*100:.1f}%')
print(f'Opaque: {opaque/total*100:.1f}%')
print(f'Corners alpha: TL={alpha[0,0]} TR={alpha[0,-1]} BL={alpha[-1,0]} BR={alpha[-1,-1]}')

# Composite on magenta for visual verification
bg = Image.new('RGBA', img.size, (255, 0, 255, 255))
bg.paste(img, (0, 0), img)
bg.save('<output_dir>/verify-magenta.png')
print('Verification image saved')
"
```

Then view the magenta verification image with the Read tool. The magenta should only show where background was removed.

### Step 4: Optional Trim

If `--trim` was requested, trim transparent padding:

```bash
source ~/.claude/tools/rembg-env/bin/activate && python3 -c "
from PIL import Image
import numpy as np

img = Image.open('<output>').convert('RGBA')
alpha = np.array(img)[:,:,3]

# Find bounding box of non-transparent pixels
rows = np.any(alpha > 0, axis=1)
cols = np.any(alpha > 0, axis=0)
rmin, rmax = np.where(rows)[0][[0, -1]]
cmin, cmax = np.where(cols)[0][[0, -1]]

# Add small padding (2% of dimensions)
pad_h = max(int(img.height * 0.02), 4)
pad_w = max(int(img.width * 0.02), 4)
rmin = max(0, rmin - pad_h)
rmax = min(img.height - 1, rmax + pad_h)
cmin = max(0, cmin - pad_w)
cmax = min(img.width - 1, cmax + pad_w)

cropped = img.crop((cmin, rmin, cmax + 1, rmax + 1))
cropped.save('<output>')
print(f'Trimmed: {img.size} -> {cropped.size}')
"
```

### Step 5: Report

```
Done: background removed
  Source: <input_path>
  Output: <output_path>
  Dimensions: <width>x<height>
  Transparent pixels: <percent>%
  Model: birefnet-general (local, offline)
```

**Verify before shipping:** open the output in Preview.app (or any viewer that shows the checkerboard pattern) to confirm real transparency. The Read tool renders transparency as solid black, so it cannot distinguish a transparent background from a black one — composite-over-a-color (Step 3) or a checkerboard viewer is the only reliable check.

## Important Rules

1. **Default to `birefnet-general`** — in side-by-side testing it produced the cleanest edges on both illustrated and photographic inputs; anime-trained models added artifacts. Only switch models if it visibly underperforms on a specific image.
2. **Always activate the venv** before running rembg or Python with Pillow/numpy.
3. **Always verify with magenta composite** — don't trust the Read tool's rendering of transparency.
4. **Never send images to external services** — rembg runs 100% locally.
5. **Preserve original files** — output to a new file, never overwrite the source.
6. **Clean up verification images** — delete the magenta composite after confirming quality.
