background-remove
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBackground 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 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 |
| 用户指令 | 执行操作 |
|---|---|
| "移除这张照片的背景" | AI移除背景,输出PNG文件 |
| "将这张图片设为透明背景" | 移除背景,保留主体 |
| "将这张图片中的产品抠出来" | 分离主体,边缘清晰 |
| "移除/photos目录下所有图片的背景" | 批量处理多张图片 |
| "快速移除背景,白色背景" | 使用快速内置方法 |
Prerequisites
前置要求
-
- AI-based background removal (recommended)
rembgbashpip install rembg # Or with GPU acceleration (faster, requires CUDA) pip install rembg[gpu] -
- Required for image processing
Pillowbashpip install Pillow
The first run will download the U2-Net model (~170MB) which is cached for future use.
-
- 基于AI的背景移除工具(推荐)
rembgbashpip install rembg # 或安装带GPU加速版本(速度更快,需要CUDA支持) pip install rembg[gpu] -
- 图像处理必备依赖
Pillowbashpip install Pillow
首次运行时会下载U2-Net模型(约170MB),下载后会缓存以便后续使用。
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 |
| 方法 | 说明 | 适用场景 |
|---|---|---|
| rembg | 基于U2-Net模型的AI处理方式 | 复杂图片、照片、产品图(默认方法) |
| builtin | 白色背景转透明 | 图标、带有干净白色背景的图形 |
Workflow
工作流程
Step 1: Gather Requirements (REQUIRED)
步骤1:收集需求(必填)
Use the tool for each question. Ask ONE question at a time.
AskUserQuestionQ1: 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
suffix (default)_nobg- 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 builtinStep 3: Deliver Result
步骤3:交付结果
- Show the result to the user
- Confirm the background was removed successfully
- Offer to:
- Process additional images
- Try a different method if quality isn't satisfactory
- Adjust output format (PNG vs WebP)
- 向用户展示处理结果
- 确认背景已成功移除
- 提供后续操作选项:
- 处理更多图片
- 若效果不佳,尝试其他方法
- 调整输出格式(PNG或WebP)
Script Parameters
脚本参数
| Parameter | Short | Description | Default |
|---|---|---|---|
| | Input image path(s) | Required |
| | Output path or directory | Auto-generated with |
| | Removal method (rembg, builtin) | rembg |
| 参数 | 简写 | 说明 | 默认值 |
|---|---|---|---|
| | 输入图片路径(可多个) | 必填 |
| | 输出路径或目录 | 自动生成,添加 |
| | 移除方法(rembg, builtin) | rembg |
Output Formats
输出格式
The output format is determined by the file extension:
| Extension | Format | Notes |
|---|---|---|
| PNG | Best quality, larger file (default) |
| WebP | Good compression, modern format |
输出格式由文件扩展名决定:
| 扩展名 | 格式 | 说明 |
|---|---|---|
| PNG | 画质最佳,文件体积较大(默认) |
| 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 rembgbash
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
-i "/path/to/image.png" \
-o "/path/to/output.png" \
-m rembgError 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.pngProcessing 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
最佳效果提示
- Use rembg for photos - AI handles complex edges (hair, fur, transparent objects)
- Use builtin for graphics - Faster for icons/logos with clean white backgrounds
- Check edges - If edges are rough, the AI method usually gives better results
- Batch process - Process multiple images at once for efficiency
- GPU acceleration - Install for faster processing on NVIDIA GPUs
rembg[gpu]
- 照片使用rembg - AI可处理复杂边缘(头发、毛发、透明物体)
- 图形使用内置方法 - 图标/Logo等带有干净白色背景的图片处理速度更快
- 检查边缘 - 若边缘粗糙,AI方法通常能提供更好的效果
- 批量处理 - 一次性处理多张图片提升效率
- GPU加速 - 安装可在NVIDIA GPU上实现更快处理
rembg[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 builtinbash
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
-i "icon.png" \
-m builtinOutput 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"