Files
AnyImage/docs/internals/operators/image-tool.md
nan e7aeeb21d4 Add tools and tests for AnyImage model management and documentation
- Implement sync_models.py for preparing and uploading ONNX artifacts.
- Create build_node.py for building and validating Blender scripts.
- Add docs.py for building and previewing documentation with Mike or MkDocs.
- Introduce model_exports.py for exporting PyTorch models to ONNX format.
- Develop pack.py for packaging the project with dependencies for different platforms.
- Add test_sync_models.py to validate sync_models functionality.
- Add test_tool_commands.py to ensure repository tools have correct command scripts.
- Initialize tools package with __init__.py.
2026-08-31 15:15:33 +08:00

2.3 KiB
Raw Blame History

Image Tool

Image Tool 从 Box、Lasso、Line 或 Polyline 手势得到一个图片 Region,再用它修改当前 Image Empty 的 Alpha。

执行过程

flowchart TD
    Tool["Box / Lasso / Line / Polyline"] --> Gesture["采集图片空间手势"]
    Gesture --> Region["RegionPath → RegionMask"]
    Region --> Refine{"Fit to Foreground?"}
    Refine -- "否" --> Mask["使用本地 RegionMask"]
    Refine -- "是" --> Job["refine-image-region"]
    Job --> Mask
    Mask --> Alpha["源 Alpha × RegionMask"]
    Alpha --> Crop["紧裁切并写回 Empty"]

四个工具共用 EditImageMask。Box 和 Lasso 可以执行 Fit to Foreground;Line 与 Polyline 始终保留几何边界,直接使用本地 RegionMask。

节点说明

选择工具并采集手势

operators/image_tool/__init__.py 汇总注册类型;box.py、lasso.py、line.py 和 polyline.py 分别定义四个 WorkspaceTool。它们都把操作交给 edit_mask.py 中的 EditImageMask。

屏幕坐标投射、点击选择、轨迹预览和状态切换来自 common/image_interaction.py。Lasso 按移动距离采点并合并近似共线点;Line 扩展为有向半平面;Polyline 在确认时闭合路径。

RegionPath → RegionMask

EditImageMask 通过 common/image_region.py 把手势整理为 RegionPath,再栅格化为 RegionMask。凹多边形使用三角剖分,自交路径使用 EVEN_ODD 填充。预览缓存只在轨迹变化时更新。

Box 和 Lasso 分别读取自己的 Fit to Foreground 设置。Line 与 Polyline 直接使用几何边界。

Fit to Foreground

未开启 Fit to Foreground 时,edit_mask.py 直接使用本地 RegionMask。开启后,RefineImageRegion 把带 Region Alpha 的局部 RGBA 提交给 server/jobs/refine_image_region.py;Job 调用 server/region/refine_region.py 运行 BEN2,并返回收紧后的图片和 bounds。

响应阶段仍由 edit_mask.py 把局部结果映射回完整画布。

源 Alpha × RegionMask

edit_mask.py 保留源 RGB,只把最终 RegionMask 乘入源 Alpha,再按非零 Alpha 紧裁切。像素读取、结果 Image 创建和共享引用处理使用 common/image_data.py。

公共 Image Empty 替换入口同步更新 empty_display_size 和 empty_image_offset,使裁切后的内容对齐原图片平面并保持对象 matrix_world。