Nodes/ComfyUI 1hewNodes/Image Mask Crop
ComfyUI Node

Image Mask Crop

Crop to the mask, keep the alpha, and get the mask back too

By 1hew·Created about a year ago·Updated 7 days ago· 33
Image Mask Crop
  • image
  • mask
  • image
  • mask
output_alphafalse
output_croptrue
pad_crop0

The inpainting workflow has a shape: crop tight around the thing you're fixing, regenerate it big, paste it back. Image Mask Crop is the first half - it crops an image down to a mask's bounding box, optionally writes that mask into the alpha channel as an RGBA output, and hands you the mask alongside. It's the node that makes "generate 1024px of a 64px problem" easy, which is exactly the resolution trick the KB's inpainting doc calls out.

It's from ComfyUI 1hewNodes (solo dev 1hew, bilingual README, active development). It pairs with Image Paste By BBox Mask in the same pack to close the loop.

How it works

Feed it an image and (optionally) a mask. If you skip the mask, it falls back to using the image's own alpha channel as the mask - handy when you're cropping cutouts. It then finds the mask's bounding box and crops the image to it. pad_crop expands the crop outward around the box, which is the classic "give the inpaint region some context" trick.

Two booleans control the output behavior:

  • output_alpha - when on, the output image is RGBA with the mask burned into the alpha channel (the input's alpha is multiplied in if it had one). When off, you get plain RGB/RGBA with no alpha math.
  • output_crop - when on (default), it crops to the bbox. Turn it off and it keeps the full image dimensions and just applies the alpha behavior - useful when you want mask-to-alpha on the whole frame.

Batch handling is the thoughtful part: image and mask counts don't have to match - they cycle against the larger batch, so one mask can crop N frames or N masks can run against one image. Outputs are the cropped image and the mask (same crop region), ready to feed a paste-back node.

The inputs that matter

  • image - source.
  • mask - optional MASK. Without it, the image's own alpha is used (and required - a no-alpha RGB image with no mask raises an error).
  • output_alpha - write the mask into the output alpha.
  • output_crop - crop to the bbox (default on).
  • pad_crop - how much context to add around the bbox.

Outputs: image, mask.

Installing it

ComfyUI Manager → search "1hew" / "ComfyUI 1hewNodes" → install → restart. Manual:

cd ComfyUI/custom_nodes
git clone https://github.com/1hew/ComfyUI-1hewNodes

Uses PIL; the pack's requirements are heavy (opencv, scikit-image, plus detection deps) so first install is slow. No models. Needs current ComfyUI (modern node API).

Gotchas

  • "mask is required" error - you fed an RGB image with no alpha and no mask. Connect a mask or give it RGBA.
  • Cropped out too much context - that's pad_crop's job; bump it when inpainted results feel cramped.
  • Different resolutions - the mask is resized to match the image before cropping, so a low-res mask gives you a slightly soft bbox. Keep masks near image resolution for precision.
Category1hewNodes/image/crop

Inputs (5)

NameTypeDefaultDescription
imageIMAGE
output_alphaBOOLEANfalse
output_cropBOOLEANtrue
pad_cropINT00–4096
maskoptMASK

Outputs (2)

NameTypeDescription
imageIMAGE
maskMASK