Crop to Mask (with Padding)
Crop to your mask, then pad the canvas like you mean it
- image
- mask
- image
- mask
- original_width
- original_height
- bbox_x
- bbox_y
- bbox_width
- bbox_height
- bbox_data
- info
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 isolationmirror/edge_extend- sample the edge of your crop and stretch or reflect it, which looks far more natural for textures than a flat fillfrom_crop- fills from the tight mask crop before padding is appliedaverage- the mean color of the region; my usual pick for texture work, because it blends into the surrounding material better than a hard black boxuse_image- only meaningful withpad_to_squareon: 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.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| mask | MASK | — | |
| padding | INT | 500–500 | Padding in pixels (expands canvas) |
| pad_to_square | BOOLEAN | false | Expand to square canvas (uses longest dimension) |
| padding_color | COMBO | black | use_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_color | STRING | #000000 | Hex color for custom padding (e.g., #FF5733) |
| invert_mask | BOOLEAN | false | Invert mask (swap black/white) |
Outputs (10)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | — |
| mask | MASK | — |
| original_width | INT | — |
| original_height | INT | — |
| bbox_x | INT | — |
| bbox_y | INT | — |
| bbox_width | INT | — |
| bbox_height | INT | — |
| bbox_data | BBOX_DATA | — |
| info | STRING | — |