Nodes/ComfyUI_Swwan/Blockify Mask (Swwan)
ComfyUI Node

Blockify Mask (Swwan)

Snap any ragged mask to a clean grid of blocks

By aining2022·Created 10 months ago·Updated about 18 hours ago· 33
Blockify Mask (Swwan)
  • masks
  • mask
◄block_size32►
◄devicecpu►

Why you'd want a chunkier mask

Detectors give you masks with ragged edges. Sometimes that's what you want and sometimes it's actively annoying: a segmentation mask that follows every pixel of a hand outline is expensive to inpaint and inconsistent frame to frame in video work, while a blocky rectangle of "this whole region, roughly" is predictable and cheap. Blockify Mask takes any mask and rounds it up to a grid - every block that contains even one masked pixel gets filled completely.

Practical uses: mosaic/pixel-art aesthetics, masks that need to land on latent-friendly boundaries, building cell-based workflows where each block is a region you'll process on its own, or just making a jittery video mask stop shimmering. It's a one-trick node, and when you need the trick there's nothing else in the graph that does it.

The mechanism

For each mask in the batch it finds the bounding box of the non-zero pixels, then divides that box into cells: divisions = bbox_size // block_size. It assigns every pixel a block id from its offset inside the bbox, then for each block asks "did anything land here?" - blocks with any content get filled solid, blocks with nothing stay zero. Everything outside the bounding box is left alone, so the original mask's position is preserved. Batches are processed one mask at a time.

Two consequences worth internalising. First, the grid is anchored to the bounding box, not to the canvas, so the same mask moved 3px to the right gets a different grid. Second, block_size is a target, not a guarantee: the real block is bbox_size // divisions wide, so you get blocks slightly larger than what you asked for, and the last column or row can be clipped by the bbox edge.

Inputs and output

  • masks - a MASK, batch or single. Everything else is optional-ish.
  • block_size - 8 to 512, default 32. The author's own tooltip: "Size of blocks in pixels (smaller = smaller blocks)". That's the knob.
  • device - cpu or gpu, default cpu. The tooltip calls it "Device to use for processing". GPU is worth trying on big batches; it only helps if your torch actually has a CUDA device.

Output is a single mask. It's a normal MASK, so it wires into anything that accepts one - mask composite, inpainting conditioning, a mask preview. Note the name is lowercase mask, not masks.

Edge cases you'll actually hit: an all-black mask is skipped (comes back black, no error). And if your block_size is bigger than the mask's bounding box, you get a single block roughly the size of the bbox - which looks like "the node did nothing" until you check block_size. Turn it down to 16 and the grid appears.

Install

It's part of ComfyUI_Swwan, so there's nothing per-node to do. Manager → search ComfyUI Swwan, or:

cd ComfyUI/custom_nodes
git clone https://github.com/aining2022/ComfyUI_Swwan
cd ComfyUI_Swwan
python -m pip install -r requirements.txt

Restart ComfyUI, refresh the browser, search Swwan in the node menu. This node needs no models and no optional extras - plain torch and numpy from the pack's base requirements. requirements.txt pulls numpy, Pillow, opencv-python, scipy and scikit-image, but not torch or torchvision; those come from your ComfyUI environment, which is the correct choice and the reason this pack doesn't nuke your CUDA setup on install.

Troubleshooting

If the node errors on a mask shape, check what you fed it. MASK in ComfyUI is [batch, height, width] with values 0–1 - a 4-channel RGBA tensor dressed up as a mask will not end up where you expect. Load Image's MASK output is 1 - alpha, so a transparent PNG gives you a nearly-full mask unless you invert; that trips people up here because blockifying an inverted mask produces a giant block equal to the whole bounding box, which reads as "the node is broken".

On CPU with a 1024px mask and block_size=8 the work is real - thousands of small slices - so if you're grinding through a long batch, raise block_size before you reach for the GPU toggle. And remember the output is still a mask, not a crop: it doesn't move or resize anything, it just fills in squares inside the original footprint.

CategorySwwan/Mask

Inputs (3)

NameTypeDefaultDescription
masksMASK—
block_sizeINT328–512Size of blocks in pixels (smaller = smaller blocks)
deviceoptCOMBOcpuDevice to use for processing

Outputs (1)

NameTypeDescription
maskMASK—