🍒Edge_Element_Cropper✀边缘元素裁剪
Edge_Element_Cropper finds the farthest pixels your mask touches and crops to them
- 原始图片
- 蒙版图片
- 裁剪后图片
- 区域测试
- 透明蒙版
Auto-crop nodes usually cut to a bounding box you supply. Edge_Element_Cropper flips that around: you give it a mask of the element you care about, and it finds the leftmost, rightmost, topmost and bottommost points the mask actually touches, then crops the image to those extremes - with an optional expand percentage so the element doesn't end up hugging the frame.
Where this earns its keep: cutting a character or object out of a scene that isn't rectangularly framed, or auto-trimming a sprite sheet where each element sits in an irregular blob. Because it derives the box from the mask's true extremes rather than a fixed region, it adapts to whatever shape the mask has. It also returns a clean transparency mask for the crop, which makes it a decent stepping stone into compositing.
How it works
You feed it 原始图片 (the image) and 蒙版图片 (a grayscale IMAGE marking the element - note: it's an IMAGE input, not a MASK type, so feed it a gray image or a converted mask). It runs the mask through OpenCV morphology (dilate then erode) to clean up noise, finds the external contours, and merges all contour points to get the four extremes. Then it builds the crop bounds: the bounding box of those extremes, expanded by 百分比扩展 (percent of the element's own size, on each side) or by at least 最小扩展像素. Negative percent shrinks instead of expands.
It then creates a fresh transparent canvas at the expanded size, pastes the cropped content in the center using the mask as alpha, and hands you three outputs: 裁剪后图片 (the RGBA crop), 区域测试 (a visualization with red contours, a blue box and L/R/T/B labels - genuinely handy for debugging), and 透明蒙版 (the alpha as a MASK).
The inputs that matter
原始图片- what gets cropped.蒙版图片- the element mask; white = element.百分比扩展- margin as a percentage of the element's size (default 0).最小扩展像素- a floor on the margin so a tiny element still gets breathing room.
Installation
Standard KimNodes pack install - Manager → search "ComfyUI_KimNodes" → Install → Restart, or git clone https://github.com/wjl0313/ComfyUI_KimNodes into custom_nodes. Needs OpenCV (cv2), which isn't in the pack's requirements.txt - if the pack won't load, pip install opencv-python ultralytics and restart (the pack imports all nodes at startup, so one missing dep hides everything).
Common issues
- "未检测到元素" (no element detected) - the mask had no contours; you'll get the original image back unchanged, plus an empty mask. Check the mask actually has white pixels and isn't mostly noise.
- Crop comes back with transparent padding - that's the expand behavior working as designed; if the extra canvas bothers you, set 百分比扩展 to 0.
- Wrong element cropped - the mask decides everything. A noisy mask with stray white specks anywhere pushes the extremes outward; clean it first (the morphology pass helps but won't fix garbage).
- Only the first image is processed - like many of these pack nodes, it takes the first frame of a batch and ignores the rest.
It's a small, focused tool, and the 区域测试 preview alone is worth having when you're dialing in crops on irregular subjects.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| 原始图片 | IMAGE | — | |
| 蒙版图片 | IMAGE | — | |
| 百分比扩展 | INT | 0-50–200 | — |
| 最小扩展像素 | INT | 00–100 | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| 裁剪后图片 | IMAGE | — |
| 区域测试 | IMAGE | — |
| 透明蒙版 | MASK | — |