ComfyUI Node

Padded Rect Mask

Padded Rect Mask Is the Little Node That Just Does It

By alexlawford·Created 2 years ago·Updated 2 years ago· 0
Padded Rect Mask
    • M
    x0
    y0
    width0
    height0
    image_width512
    image_height512
    padding0

    Type four numbers, get a rectangle mask

    Every inpainting and compositing workflow needs a mask sooner or later - a white shape on black that tells the model "regenerate here" or "leave this alone." Usually you paint it by hand or lean on a detector pack to produce it. Padded Rect Mask is the version for when you already know the region and you just want to type it: x, y, width, height, a bit of padding, and out comes a MASK. No brush, no model, no dependencies. One node, one output, and a name that tells you exactly what its one trick is.

    That trick is padding, and it matters more than it sounds. In the ground-then-mask workflows the community has settled on, a VLM or detector hands you a bounding box around an object - and those boxes come tight. They hug the cup and not the saucer, as one inpainting workflow author put it, so you pad the box out before you inpaint or the model invents clutter to fill the space. This node is that padding step as a dedicated tool. It's also the quick way to build a "keep this region intact" mask: invert it (core InvertMask) and feed it to SetLatentNoiseMask, and your sampler re-denoises everything except the rectangle.

    How it works (it's three lines of torch)

    The source is small enough to read in a minute. create_mask makes a torch.zeros((image_height, image_width)) canvas, sets the rectangle to 1.0, and reshapes it to the standard (batch, height, width) MASK format ComfyUI expects. Coordinates come from the top-left corner like a bounding box, and padding expands the rect on all four sides. Two quirks are worth knowing because they're real, visible behavior:

    • Padding silently dies near the top-left corner. min_x = x - padding if x - padding > 0 else 0 - if the padded rect would start above the image, it clamps to 0 and drops that side's padding rather than shifting the rect to fit. Fine for most work; surprising if you pad a box sitting at the origin and wonder why it looks lopsided.
    • It's one pixel bigger than you asked. The fill includes max_x and max_y, so the masked region is width + 2*padding + 1 wide. A hair off, rarely matters, good to know before you pixel-hunt a seam.
    • It clips instead of crashing. There's no right/bottom clamp, but Python slicing past the array end just stops at the edge - a rect running off the canvas fills to the border. It also means this is not an outpaint tool: the mask can never extend beyond image_width/image_height.

    The inputs that matter

    All seven are integers, and all have defaults, so you can drop the node and just fiddle:

    • x / y - the rectangle's top-left corner in pixels.
    • width / height - how big the rect is.
    • image_width / image_height - the size of the canvas the mask lives in. Must match your actual image or latent, or the mask will be offset/mis-sized against the pixels you're masking.
    • padding - expand the rect by N pixels on every side. Default 0; for inpainting detail passes, the community's rule of thumb is 32–64px, and more if your mask will be feathered.

    The single MASK output wires straight into anything that eats a mask: SetLatentNoiseMask, InpaintModelConditioning, ImageCompositeMasked, or a MaskToImage if you want to preview it as a black-and-white image.

    Installing it

    cd ComfyUI/custom_nodes
    git clone https://github.com/alexlawford/Comfyui-Padded-Rect-Mask
    

    then restart ComfyUI. Or use ComfyUI Manager and search "Comfyui-Padded-Rect-Mask." That's genuinely it - the requirements.txt is empty (the only dependency is torch, which ComfyUI already runs on), and there are no model downloads or heavy extras. This is one of the rarer one-step installs.

    Gotchas

    • You won't find it by browsing. The author left the template's default category in, so the node appears under a menu category literally called CustomNodesTemplate. Search the node picker for "Padded Rect Mask" by name instead.
    • Mask doesn't line up - check image_width/image_height against your image first; the 512 defaults are almost never right for an SDXL or Flux canvas.
    • Hard edges. There's no feather input here. Run the MASK through a core FeatherMask or GrowMask (or InvertMask for the inverse) rather than expecting softness from this node.
    • Don't expect updates. It's a single node minted from the official custom-node template, with essentially zero community footprint as of mid-2026. Upside: it's pure torch, so there's no fragile pinned dependency to rot underneath you.

    If your job is "I have the coordinates, give me a padded rectangle mask," this is the node. It won't blow your mind, but it saves you the small branch of graph you'd otherwise build to do the same thing with three core nodes.

    CategoryCustomNodesTemplate

    Inputs (7)

    NameTypeDefaultDescription
    xINT0
    yINT0
    widthINT0
    heightINT0
    image_widthINT512
    image_heightINT512
    paddingINT0

    Outputs (1)

    NameTypeDescription
    MMASK