mcpField
B
background-remove

background-remove

github.com/michaelboeding创意与媒体3055 次安装2 周前更新
代码仓库

关于 background-remove

background-remove 是用于从单张或多张图片中移除背景的技能,提供 rembg AI 处理和内置白色背景转换两种方式,并可输出透明 PNG 或 WebP。

主要功能

  1. 01使用 rembg 和 U2-Net 去除背景
  2. 02内置白色背景转透明方法
  3. 03支持多张图片批量处理
  4. 04输出透明 PNG 或 WebP

适用场景

  1. 01为产品照片抠出主体
  2. 02将白底图标转为透明背景
  3. 03批量处理照片目录

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/michaelboeding/skills --skill background-remove

安装后在 Claude Code 中输入/background-remove确认已加载。

附带文件scripts/background_remove.py

详细介绍

背景移除技能

使用 AI(rembg/U2-Net)或内置方法移除图像背景。

输出: 透明背景的 PNG 或 WebP。

快速示例

用户说执行操作
“移除这张照片的背景”使用 AI 移除背景,输出 PNG
“让这张图片变透明”移除背景,保留主体
“从这张图片中抠出产品”分离主体并保留清晰边缘
“移除 /photos 中所有图片的背景”批量处理多张图片
“快速移除背景,换成白色背景”使用快速内置方法

先决条件

  • rembg - 基于 AI 的背景移除工具(推荐)

    pip install rembg
    # Or with GPU acceleration (faster, requires CUDA)
    pip install rembg[gpu]
    
  • Pillow - 图像处理必需

    pip install Pillow
    

首次运行时会下载 U2-Net 模型(约 170MB),之后会缓存以供后续使用。

方法

方法描述适用场景
rembg使用 U2-Net 模型进行 AI 背景移除复杂图像、照片、产品图(默认)
builtin将白色转换为透明图标、带干净白色背景的图形

工作流

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

针对每个问题使用 AskUserQuestion 工具。每次只问一个问题。

问题 1:图像来源

“要移除哪些图像的背景?

请提供文件路径或粘贴图像。”

等待回复。

问题 2:方法(可选)

“选择哪种移除方法?

  • AI(rembg)- 质量最佳,适用于任何图像(默认)
  • 内置 - 速度更快,最适合白色背景”

等待回复。若用户未指定,则默认使用 AI。

问题 3:输出位置(可选)

“将结果保存到哪里?

  • 保存在相同位置,并添加 _nobg 后缀(默认)
  • 自定义路径”

等待回复。

步骤 2:执行背景移除

单张图像:

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

批量处理:

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"

使用内置方法(处理白色背景更快):

python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "/path/to/icon.png" \
  -m builtin
步骤 3:交付结果
  1. 向用户展示结果
  2. 确认背景已成功移除
  3. 提议:
    • 处理更多图像
    • 如果质量不理想,尝试其他方法
    • 调整输出格式(PNG 或 WebP)

脚本参数

参数简写描述默认值
--input-i输入图像路径(可多个)必需
--output-o输出路径或目录自动生成,并添加 _nobg 后缀
--method-m移除方法(rembg、builtin)rembg

输出格式

输出格式由文件扩展名决定:

扩展名格式说明
.pngPNG质量最佳,文件较大(默认)
.webpWebP压缩效果好,现代格式

与其他技能集成

其他需要移除背景的技能可以调用此技能:

从 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']}")
从命令行调用
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "/path/to/image.png" \
  -o "/path/to/output.png" \
  -m rembg

错误处理

未安装 rembg:

rembg not installed. Install with: pip install rembg[gpu] (or pip install rembg for CPU-only)

脚本会自动回退到内置方法。

未找到图像:

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

处理失败:

  • 尝试其他方法
  • 检查图像文件是否损坏
  • 确保有足够的内存处理大型图像

获得最佳效果的提示

  1. 照片使用 rembg - AI 能处理复杂边缘(头发、毛发、透明物体)
  2. 图形使用 builtin - 处理带干净白色背景的图标或徽标时速度更快
  3. 检查边缘 - 如果边缘粗糙,AI 方法通常能提供更好的结果
  4. 批量处理 - 一次处理多张图像,提高效率
  5. GPU 加速 - 安装 rembg[gpu],在 NVIDIA GPU 上加快处理速度

示例

移除照片背景
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "product_photo.jpg" \
  -o "product_transparent.png"
批量处理文件夹
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i photos/*.jpg \
  -o "transparent_photos/"
快速移除图标背景(白色背景)
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "icon.png" \
  -m builtin
输出为 WebP(文件更小)
python3 ${SKILL_PATH}/skills/background-remove/scripts/background_remove.py \
  -i "photo.jpg" \
  -o "result.webp"

评论

支持 Markdown