实例商品裁切为背景图
Crop, Center, and Square Up Every Product Onto White
- segmented_images
- masks
- original_image
- products
- manifest
- product_count
- original_background
If you run a marketplace shop, you know the drill: every listing needs a square product image on white, product centered, nothing else in frame. KSProductsToBackground is that step, automated. Feed it segmented product instances plus their masks from the SAM pipeline, and it crops each one tight, centers it on a white or transparent square, and hands you a clean batch - plus a manifest telling you which instance became which output.
What it does
It's the "make it listing-ready" node, and it's the payoff of the whole detect→segment chain:
segmented_images+masks- the batch fromKSSAMSegmentByBoxes(orKSGroundedSAMSegment). Each mask defines one product instance.background_mode-whiteortransparentoutput.output_size(default 1024, range 256–4096) - the square canvas edge length. Bigger = more detail, bigger files, slower.margin_percent(10) - padding around the product. 0 fills the canvas edge-to-edge; higher shrinks the product in the frame.sort_order-top_to_bottomnumbers outputs reading down-then-across;left_to_rightreads across-then-down. Matters if the order needs to match a catalog sheet.edge_feather(0.8) and the same mask-cleanup knobs asKSApplyMaskBackground(mask_threshold,min_mask_component_area,mask_close_radius,mask_expand_pixels,fill_mask_holes).
The interesting optional input is original_image. When you connect it, the node crops each product from the original photo using the mask as a guide instead of from the (already-blackened) segmented image. The tooltip says it plainly: the original usually keeps more real texture and color. If your segmented crops look washed out, this is the fix.
There's also prefer_grouped_product_masks (default on): when several small parts sit under one big mask, it keeps the big product mask whole instead of fragmenting into pieces. Off, and you get more separate components - which you might want for multi-part products.
The outputs
products- the batch of centered square crops.manifest- JSON recording product count, output index, source instance index, and each original crop box. Genuinely handy if you need to trace outputs back to inputs.product_count- how many made it through.original_background- one white/transparent full-canvas image with all detected products kept together. That's the "group shot" version, complementing the individual squares.
Installing
With the pack:
cd ComfyUI/custom_nodes
git clone https://github.com/kongshan4219/ComfyUI-Kongshan-Nodes
restart ComfyUI. Local, no keys; needs the SAM models only if you're using the upstream segmentation nodes in the same pack.
Gotchas
- The "cutout" is only as clean as the mask. This node crops by mask, so a feathered or sloppy mask produces a halo. Use
mask_thresholdup andedge_featherlow for crisp catalog edges. - 1024 default × many products adds up. If you're producing 20 listing images, that's 20 × 1024² PNGs. Drop
output_sizefor drafts, raise it only for finals. - The
1688shape of it.prefer_grouped_product_masksand the square-white-centering are tuned for Alibaba-style marketplace listings, which is the author's world (this pack is extracted from their private e-commerce workspace). If that's your world, this node is genuinely the fastest path from a messy photo to a clean catalog sheet.
Young, tiny, unknown pack - but this is one of its two or three genuinely differentiated nodes. There's nothing else in the typical ComfyUI install that does "SAM masks → centered square white-background product batch + manifest" in one node.
Inputs (14)
| Name | Type | Default | Description |
|---|---|---|---|
| segmented_images | IMAGE | 分割后的商品实例图片批次,通常来自 SAM 分割节点。未连接 original_image 时会作为裁切来源。 | |
| masks | MASK | 每个商品实例对应的遮罩批次。遮罩越准确,裁切边缘越干净。 | |
| background_mode | COMBO | 背景模式。white 输出白底商品图;transparent 输出带 alpha 通道的透明底商品图。 | |
| output_size | INT | 1024256–4096 | 每个输出商品图的正方形边长。值越大细节越多但文件更大、处理更慢。 |
| margin_percent | FLOAT | 10.00–40 | 商品四周留边比例。0 会尽量铺满画布;数值越大,商品越小、留边越多。 |
| edge_feather | FLOAT | 0.80–8 | 遮罩边缘羽化半径。0 保持硬边;数值越大边缘越柔和。 |
| sort_order | COMBO | 输出排序。top_to_bottom 按从上到下、再从左到右编号;left_to_right 按从左到右、再从上到下编号。 | |
| original_imageopt | IMAGE | 原始完整图片。连接后会从原图裁切商品,通常比使用分割图保留更多真实纹理和颜色。 | |
| mask_thresholdopt | FLOAT | 0.500.05–0.95 | 遮罩二值化阈值。降低会保留更多边缘/半透明区域;提高会收紧主体范围。 |
| min_mask_component_areaopt | INT | 640–4096 | 最小连通区域面积。0 不过滤;值越大越容易去掉小噪点,也可能误删小商品部件。 |
| mask_close_radiusopt | INT | 40–32 | 遮罩闭运算半径。0 不处理;值越大越能连接小缝隙、平滑边缘,但可能粘连相邻物体。 |
| mask_expand_pixelsopt | INT | 20–32 | 遮罩向外扩展像素数。0 不扩展;值越大越能避免裁掉边缘,也越可能带入背景。 |
| fill_mask_holesopt | BOOLEAN | true | 是否填充遮罩内部空洞。开启适合实心商品;关闭适合保留真实镂空区域。 |
| prefer_grouped_product_masksopt | BOOLEAN | true | 多个小部件被一个大遮罩覆盖时,优先保留完整商品大遮罩。关闭后会保留更多单独部件。 |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| products | IMAGE | — |
| manifest | STRING | — |
| product_count | INT | — |
| original_background | IMAGE | — |