Nodes/ComfyUI-TextureAlchemy/Crop to Mask (with Padding)
ComfyUI Node

Crop to Mask (with Padding)

Crop to your mask, then pad the canvas like you mean it

By amtarr·Created 9 months ago·Updated 4 months ago· 58
Crop to Mask (with Padding)
  • image
  • mask
  • image
  • mask
  • original_width
  • original_height
  • bbox_x
  • bbox_y
  • bbox_width
  • bbox_height
  • bbox_data
  • info
padding50
pad_to_squarefalse
padding_colorblack
custom_color#000000
invert_maskfalse

When you crop an image to a mask's bounding box, you usually don't want a knife-edge cut right at the mask boundary. Diffusion models hate it, inpainting hates it, and visually it looks like a box. This node crops to the mask bounds and then expands the canvas with padding, giving the region breathing room. The padding isn't just including more of the source image - it's genuinely new pixels, and you get to choose what fills them.

That distinction is the core of the node. padding is in real pixels (default 50), and padding_color decides what those new pixels are:

  • black / white - solid fills, good for clean background isolation
  • mirror / edge_extend - sample the edge of your crop and stretch or reflect it, which looks far more natural for textures than a flat fill
  • from_crop - fills from the tight mask crop before padding is applied
  • average - the mean color of the region; my usual pick for texture work, because it blends into the surrounding material better than a hard black box
  • use_image - only meaningful with pad_to_square on: it grabs a square centered on the mask from the full input image, so you get real pixels with no mirroring or solid fill. That's the option for "I want actual content around this mask."

There's also pad_to_square, which expands to a square canvas using the longest dimension - essential when the crop is going into a model or a texture slot that wants square inputs. And invert_mask swaps black and white before the bounding box is computed, which is how you crop to the outside of a selection.

The outputs are the useful part for round-trip workflows. You get the cropped image and matching mask, plus original_width, original_height, and the tight bbox_x/y/width/height of the mask region, and finally a bbox_data object and an info string. That bbox output is the handshake: feed it to the pack's Crop (from Bbox Data) node later and you can re-crop or restore around the exact same region. The info string is just human-readable crop stats.

Mechanically it computes the mask's bounding box (with some nice console diagnostics about mask coverage and edge values to help you debug thin or stray masks), resizes the mask to match the image if they disagree, and applies the chosen padding fill. It's all tensor slicing and torch ops, so it's instant even at high resolutions.

Where this shines: inpainting. Crop a damaged region with a mask, get a padded square around it, run your inpaint, then use the bbox info to paste the result back. It's also great for isolating a single texture element - a brick, a rock, a leaf - from a larger photo before you tile or extract it.

Install: Manager → search "Texture Alchemy", or:

cd ComfyUI/custom_nodes
git clone https://github.com/amtarr/ComfyUI-TextureAlchemy

Restart, then Texture Alchemist/Texture → Crop to Mask (with Padding). No extra dependencies. The README's copy-a-folder instruction is stale (old pack name), and if Manager blocks a Git-URL install, clone by hand rather than lowering the security level.

CategoryTexture Alchemist/Texture

Inputs (7)

NameTypeDefaultDescription
imageIMAGE
maskMASK
paddingINT500–500Padding in pixels (expands canvas)
pad_to_squareBOOLEANfalseExpand to square canvas (uses longest dimension)
padding_colorCOMBOblackuse_image (with pad to square): crop a square from the full input, centered on the mask—real image pixels, no solid/mirror. Other modes: from_crop/edge/mirror = fill from the tight mask crop; black/white/average/custom = solid/avg
custom_colorSTRING#000000Hex color for custom padding (e.g., #FF5733)
invert_maskBOOLEANfalseInvert mask (swap black/white)

Outputs (10)

NameTypeDescription
imageIMAGE
maskMASK
original_widthINT
original_heightINT
bbox_xINT
bbox_yINT
bbox_widthINT
bbox_heightINT
bbox_dataBBOX_DATA
infoSTRING