Nodes/ComfyUI-ZML-Image/ZML_限制纯色背景大小
ComfyUI Node

ZML_限制纯色背景大小

Chop the dead background pixels off your images before stitching

By zml-w·Created about a year ago·Updated 2 months ago· 218
ZML_限制纯色背景大小
  • 图像
  • 图像
处理模式
背景颜色
阈值10
矩形保留像素0
不规则形状保留像素50
透明图像添加背景
自定义背景颜色#000000

Ever render a 3-view character sheet on a white background, then tried to stitch the three views into one image - and had to manually crop each one because they're all slightly different sizes? That's exactly what this node is for. It detects the pure-color background around your subject, crops away the empty margin, and leaves you with tight bounding boxes you can merge. The author built it as the cleanup half of a two-step flow: a "constrain aspect ratio" node pads your image to a ratio, and this one strips the padding back off.

How it works

Under the hood it's a simple but effective pixel test. You pick a background color, and the node marks every pixel whose color distance from that target is within a 阈值 (threshold, default 10) as background. Everything else is subject. Then it crops in one of two ways:

  • 矩形 (rectangle) - finds the smallest axis-aligned box containing all non-background pixels. Clean, predictable, great for stitching.
  • 不规则形状 (irregular) - crops down to the actual subject silhouette. You keep "矩形保留像素" (rectangle margin) or "不规则形状保留像素" (irregular margin) of breathing room so the subject doesn't get glued to the edge.

For transparent images it skips color matching entirely and just looks at the alpha channel - background is where alpha is zero. There's also a "透明图像添加背景" option to lay a white or green mat behind a transparent image before cropping, which is handy if your pipeline expects RGB.

The inputs that matter

  • 背景颜色 - 白色, 黑色, 绿色, 透明, or 自定义. For custom, feed a hex like #RRGGBB into the optional 自定义背景颜色 input.
  • 处理模式 - 矩形 or 不规则形状, as above.
  • 阈值 - how far from the target color still counts as background. 10 handles most renders; raise it if your "white" has JPEG-ish noise, lower it if you're getting content eaten.
  • The two margin fields - 0 for tightest rectangle crop, or ~50 for the irregular mode's default.

Output is 图像, and it's list-capable - feed it a batch of character views and each comes out individually cropped.

Installing

Same pack, same steps:

cd ComfyUI/custom_nodes
git clone https://github.com/zml-w/ComfyUI-ZML-Image

restart, or use ComfyUI Manager. English UI needs the translation patch (https://github.com/zml-w/ZZZ_ZML_English_Patch). It uses Pillow/numpy from the pack's base deps - nothing extra.

Where people get burned

Threshold is the classic trap. A pure-white render at threshold 10 is fine; a photo-like background or a heavily compressed image will leave a 1–2px gray halo that breaks seamless stitching - bump the threshold and re-run. And "irregular" mode with a low margin can shave stray pixels off hair or wings, because it's working off the strict silhouette, not any content awareness. Check the output once before you batch a hundred images through it. This is a genuinely useful node, and the author's example flow (constrain ratio → crop pure background → merge) is the intended use; on its own it just trims margins, which is fine too.

Categoryimage/ZML_图像/图像

Inputs (8)

NameTypeDefaultDescription
图像IMAGE
处理模式COMBO2 options: 矩形, 不规则形状
背景颜色COMBO5 options: 白色, 黑色, 绿色, 透明, 自定义
阈值INT100–255
矩形保留像素INT00–512
不规则形状保留像素INT500–512
透明图像添加背景COMBO3 options: 无, 白色, 绿色
自定义背景颜色optSTRING#000000

Outputs (1)

NameTypeDescription
图像IMAGE