Nodes/ComfyUI_KimNodes/🍒IconDistributeByGrid / 区域分发图标
ComfyUI Node

🍒IconDistributeByGrid / 区域分发图标

Fill a mask region with icons, smartly, and get a JSON map back

By wjl0313·Created 2 years ago·Updated 12 months ago· 54
🍒IconDistributeByGrid / 区域分发图标
  • scene_image
  • mask_image
  • icons
  • processed_image
  • updated_mask
  • output_json
icon_size256
min_icon_size128
icon_count5
min_distance20
edge_padding0
spacing_factor1.0
random_seed-1
enable_rotationfalse
input_json
rotation_range360

The "grid" in the name is a little misleading - IconDistributeByGrid doesn't put icons on a regular grid at all. It's the pack's smarter scatter tool: it places icon_count icons inside the white region of a mask, shrinking them until they all fit, keeping them apart, and - the useful part - it hands back a machine-readable JSON describing exactly where every icon landed. That JSON is what the pack's JSON_Image_Compositor consumes to reassemble things, and it also serves as the reproducible record of a layout.

Compared to its sibling Distribute_Icons (which is pure random), this one is deterministic when you want it to be: random_seed is there, and the placement tries harder to fill the region evenly. If you're making sticker sheets, app icon mockups, or any "decorate this bounded area" job where you might want to revisit the same layout, this is the one to reach for.

How it works

You feed scene_image, mask_image (white = allowed), and icons (a batch). The node finds the valid area - mask white minus an optional edge_padding buffer derived from Canny edges - then runs a placement loop that's genuinely clever: it starts at icon_size, tries to place icons by sampling candidate spots and scoring them for distance from already-placed icons, and if the round fills up, it shrinks the icon size by 20% and goes again until it either places icon_count or hits min_icon_size. Each placed icon is pasted with its alpha, and the node records it in the output JSON: the icon crop as a base64 image, its bbox as four "x,y" corner strings, size, rotation, and position id. It also returns an updated_mask where placed-icon pixels are black on white - handy as a "what's taken" overlay.

The inputs that matter

  • icon_size / min_icon_size - the start and floor for the shrink-to-fit loop.
  • icon_count - how many icons to place (the loop stops when it runs out of room, with a warning).
  • min_distance / edge_padding / spacing_factor - spacing controls; spacing_factor scales the gap, 1.0 = standard.
  • random_seed - -1 = random each run; any other value = reproducible.
  • enable_rotation + rotation_range - optional random rotation (range in degrees).
  • input_json - optional; if you pass the JSON from a previous run, it appends new icons to the existing layout instead of starting fresh.

Outputs: processed_image (the composited scene), updated_mask (placement mask), and output_json (the JSON string - feed it straight into JSON_Image_Compositor).

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) for the mask/edge work; not in the pack's requirements.txt, so if the pack doesn't appear, pip install opencv-python ultralytics and restart.

Common issues

  • "没有足够的有效区域" (not enough valid area) - the mask region is too small for even the minimum icon size. Enlarge the mask or lower min_icon_size.
  • Fewer icons than requested - the shrink loop gives up at min_icon_size; the warning message tells you how many it couldn't place. Raise the mask area or lower the floor.
  • Icons overlap the mask edge - edge_padding exists exactly for this; set it to a few pixels.
  • Only the first scene and mask are used - by design, it says so in the code comments. Feed single images.

This node is the heart of the pack's icon pipeline - learn it and the JSON output becomes the connective tissue for the whole flow.

Category🍒 Kim-Nodes/🧩Icon Processing | 图标处理

Inputs (13)

NameTypeDefaultDescription
scene_imageIMAGE
mask_imageMASK
iconsIMAGE
icon_sizeINT25664–512
min_icon_sizeINT12864–256
icon_countINT51–100
min_distanceINT200–200
edge_paddingINT00–100
spacing_factorFLOAT1.00.5–2
random_seedINT-1-1–2147483647
enable_rotationBOOLEANfalse
input_jsonoptSTRING
rotation_rangeoptINT3600–360

Outputs (3)

NameTypeDescription
processed_imageIMAGE
updated_maskMASK
output_jsonSTRING