Files
SnapOtter/apps/docs/zh-CN/tools/image/smart-crop.md
T
SnapOtterandGitHub 4963ab3bbd feat(docs-i18n): translate all documentation into 20 languages
All 181 docs markdown files translated into 20 languages (apps/docs/<locale>/**). Companion to the i18n code PR; admin-merged because the file count exceeds GitHub's per-PR CI trigger limit. Validated by pnpm i18n:check (all surfaces, 0 stale/missing) and a clean all-locale docs build.
2026-07-11 13:52:47 +08:00

3.6 KiB
Raw Blame History

description, i18n_source_hash, i18n_provenance, i18n_output_hash
description i18n_source_hash i18n_provenance i18n_output_hash
基于主体、人脸和熵的裁剪,使用 Sharp 和 AI 人脸检测智能地为图像取景。 acbe1439c6d8 human f0dcbc7bcece

智能裁剪

智能的主体感知、人脸感知或基于修剪的裁剪。使用 Sharp 的注意力/熵策略和 AI 人脸检测实现智能取景。

API 端点

POST /api/v1/tools/image/smart-crop

处理方式: 异步(返回 202,通过 SSE 轮询 /api/v1/jobs/{jobId}/progress 获取状态)

模型包: face-detection200-300 MB- 仅 face 模式需要

参数

参数 类型 必填 默认值 说明
file file - 图像文件(multipart
mode string "subject" 裁剪模式:subjectfacetrim。(旧值 attentioncontent 分别映射到 subjecttrim
strategy string "attention" 主体模式的策略:attentionentropy
width integer - 目标宽度(像素)
height integer - 目标高度(像素)
padding integer 0 主体周围的内边距百分比(0-50
facePreset string "head-shoulders" 人脸取景预设:closeuphead-shouldersupper-bodyhalf-body
sensitivity number 0.5 人脸检测灵敏度(0-1
threshold integer 30 修剪模式下的背景检测阈值(0-255
padToSquare boolean false 将修剪后的结果填充为正方形
padColor string "#ffffff" 填充用的背景颜色
targetSize integer - 填充输出的目标尺寸(像素)
quality integer - 输出质量(1-100

示例请求

curl -X POST http://localhost:1349/api/v1/tools/image/smart-crop \
  -F "file=@portrait.jpg" \
  -F 'settings={"mode":"face","width":1080,"height":1080,"facePreset":"head-shoulders"}'

响应

初始响应(202 Accepted

{
  "jobId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  "async": true
}

进度(SSE,位于 /api/v1/jobs/{jobId}/progress

event: progress
data: {"phase":"processing","percent":50}

最终结果(通过 SSE

{
  "phase": "complete",
  "percent": 100,
  "result": {
    "jobId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
    "downloadUrl": "/api/v1/download/{jobId}/portrait_smartcrop.jpg",
    "originalSize": 500000,
    "processedSize": 320000
  }
}

模式

主体模式

使用 Sharp 的注意力或熵策略找到视觉上最有趣的区域,并围绕它进行裁剪。

人脸模式

使用 AI 检测人脸,然后根据指定的 facePreset 围绕检测到的人脸取景裁剪。如果未检测到人脸,则回退到主体模式(注意力策略)。

修剪模式

移除图像中均匀的边框/背景。可选地使用指定的背景颜色和目标尺寸将结果填充为正方形。

说明

  • 该工具使用带 executionHint: "long"createToolRoute 工厂,因此返回 202 并附带 SSE 进度。
  • 人脸模式需要 face-detection 模型包(200-300 MB)。
  • 主体模式和修剪模式无需任何 AI 模型包即可工作。
  • facePreset 决定裁剪对检测到的人脸取景的紧密程度:closeup 最紧凑,half-body 最宽松。
  • 如果未指定宽度/高度,则默认为 1080x1080。