background-remove

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Background Remove Skill

背景移除Skill

Remove backgrounds from images using AI (rembg/U2-Net) or built-in methods.
Output: PNG or WebP with transparent background.
借助AI(rembg/U2-Net)或内置方法移除图片背景。
输出: 带透明背景的PNG或WebP格式文件。

Quick Examples

快速示例

User SaysWhat 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
用户指令执行操作
"移除这张照片的背景"AI移除背景,输出PNG文件
"将这张图片设为透明背景"移除背景,保留主体
"将这张图片中的产品抠出来"分离主体,边缘清晰
"移除/photos目录下所有图片的背景"批量处理多张图片
"快速移除背景,白色背景"使用快速内置方法

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.
  • rembg
    - 基于AI的背景移除工具(推荐)
    bash
    pip install rembg
    # 或安装带GPU加速版本(速度更快,需要CUDA支持)
    pip install rembg[gpu]
  • Pillow
    - 图像处理必备依赖
    bash
    pip install Pillow
首次运行时会下载U2-Net模型(约170MB),下载后会缓存以便后续使用。

Methods

处理方法

MethodDescriptionBest For
rembgAI-based using U2-Net modelComplex images, photos, products (default)
builtinWhite-to-transparent conversionIcons, graphics with clean white backgrounds
方法说明适用场景
rembg基于U2-Net模型的AI处理方式复杂图片、照片、产品图(默认方法)
builtin白色背景转透明图标、带有干净白色背景的图形

Workflow

工作流程

Step 1: Gather Requirements (REQUIRED)

步骤1:收集需求(必填)

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.
使用
AskUserQuestion
工具逐个询问问题,每次仅问一个问题。
问题1:图片来源
"需要移除哪张/哪些图片的背景?
请提供文件路径或粘贴图片。"
等待用户回复。
问题2:处理方法(可选)
"选择哪种移除方法?
  • AI(rembg)- 效果最佳,适用于所有图片(默认)
  • 内置方法 - 速度更快,适用于白色背景图片"
等待用户回复。若用户未指定,默认使用AI方法。
问题3:输出位置(可选)
"结果保存到哪里?
  • 与原文件同目录,文件名添加
    _nobg
    后缀(默认)
  • 自定义路径"
等待用户回复。

Step 2: Execute Background Removal

步骤2:执行背景移除

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
单张图片处理:
bash
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "/path/to/image.jpg" \
  -o "/path/to/output.png"
批量处理:
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"
使用内置方法(白色背景图片处理更快):
bash
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "/path/to/icon.png" \
  -m builtin

Step 3: Deliver Result

步骤3:交付结果

  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)
  1. 向用户展示处理结果
  2. 确认背景已成功移除
  3. 提供后续操作选项:
    • 处理更多图片
    • 若效果不佳,尝试其他方法
    • 调整输出格式(PNG或WebP)

Script Parameters

脚本参数

ParameterShortDescriptionDefault
--input
-i
Input image path(s)Required
--output
-o
Output path or directoryAuto-generated with
_nobg
suffix
--method
-m
Removal method (rembg, builtin)rembg
参数简写说明默认值
--input
-i
输入图片路径(可多个)必填
--output
-o
输出路径或目录自动生成,添加
_nobg
后缀
--method
-m
移除方法(rembg, builtin)rembg

Output Formats

输出格式

The output format is determined by the file extension:
ExtensionFormatNotes
.png
PNGBest quality, larger file (default)
.webp
WebPGood compression, modern format
输出格式由文件扩展名决定:
扩展名格式说明
.png
PNG画质最佳,文件体积较大(默认)
.webp
WebP压缩效果好,现代图片格式

Integration with Other Skills

与其他Skill集成

This skill can be called by other skills that need background removal:
本Skill可被其他需要背景移除功能的Skill调用:

From Python (import)

Python调用(导入)

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']}")
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"已保存至: {result['file']}")
else:
    print(f"错误: {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
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
未安装rembg:
未安装rembg。请执行以下命令安装:pip install rembg[gpu](或pip install rembg仅支持CPU版本)
脚本会自动 fallback 到内置方法。
图片未找到:
图片未找到: /path/to/image.png
处理失败:
  • 尝试更换处理方法
  • 检查图片文件是否损坏
  • 确保处理大图片时有足够内存

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
  1. 照片使用rembg - AI可处理复杂边缘(头发、毛发、透明物体)
  2. 图形使用内置方法 - 图标/Logo等带有干净白色背景的图片处理速度更快
  3. 检查边缘 - 若边缘粗糙,AI方法通常能提供更好的效果
  4. 批量处理 - 一次性处理多张图片提升效率
  5. GPU加速 - 安装
    rembg[gpu]
    可在NVIDIA GPU上实现更快处理

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"
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/"
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
bash
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "icon.png" \
  -m builtin

Output as WebP (smaller file size)

输出为WebP格式(文件体积更小)

bash
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "photo.jpg" \
  -o "result.webp"
bash
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "photo.jpg" \
  -o "result.webp"