Nodes/ComfyUI_BadgerTools/CropImageByMask-badger
ComfyUI Node

CropImageByMask-badger

Crop to whatever your mask covers — no coordinates typed by hand

By AbyssBadger0·Created 3 years ago·Updated 2 years ago· 8
CropImageByMask-badger
  • image
  • mask
  • cropped_img
  • X
  • Y

Every serious ComfyUI user ends up needing the same thing eventually: crop the image down to just that one object. You've got a face, a character, a product - you don't know its exact pixel coordinates, and you don't want to hardcode them either. CropImageByMask-badger is the "just do it" answer: point it at an image and a mask, and it crops the image tight to the mask's bounding box. No typing, no guessing.

This is the exact technique that keeps masked inpainting fast. The standard workflow is: crop the region small, render it at the model's native resolution where you get full generation budget on a tiny area, then paste it back. A 64px eye gets 1024px of detail budget because you cropped first. That's the crop-and-stitch pattern from the KB's inpainting essay, and this node is the crop half of it.

How it works

The math is dead simple and that's the appeal. The mask is binarized (anything above 0.5 counts as mask), the code finds the bounding box of all the nonzero pixels - topmost, leftmost, bottommost, rightmost - and slices the image tensor to that box. You get the tightest rectangle that contains everything the mask covers, plus the box's top-left corner coordinates.

Inputs and outputs

  • image - the IMAGE you want cropped.
  • mask - the MASK that defines the region. It needs to match the image's size, like most mask operations in this pack.

Three outputs:

  • cropped_img - the tight crop, ready to feed an upscaler, a sampler, or a VAE.
  • X - the left edge of the crop, in pixels.
  • Y - the top edge of the crop, in pixels.

Those X/Y values are the useful hidden half. Keep them around and you can paste the cropped-and-rendered result back onto the original at exactly the right spot with something like ImageOverlap-badger (same pack) or a compositing node. That's the full crop-edit-paste loop, coordinates included for free.

Installing it

This is one of ~40 nodes in ComfyUI_BadgerTools. Through ComfyUI Manager, search "BadgerTools" and install, or clone:

cd ComfyUI/custom_nodes
git clone https://github.com/AbyssYuan0/ComfyUI_BadgerTools

Restart ComfyUI after either route. No model downloads for this node. The pack does pull heavy requirements (open_clip_torch, sentence_transformers, moviepy, scikit-image) at startup even though this node only needs Pillow and torch - expect a slow first launch. It's a personal utility pack, so the docs are thin; the code is the documentation.

Gotchas

  • An empty mask is an error. If the mask sums to zero the node raises "The mask is empty, cannot determine bounding box for cropping." Check your mask isn't fully black before wiring this in.
  • Mask size must match image size. The crop is done on the image tensor using mask coordinates, so mismatched resolutions give you garbage or an exception.
  • It's a rectangle, not a shape. The crop is the mask's bounding box, not the mask silhouette. You'll want ApplyMaskToImage-badger for actually cutting out the shape; this one just frames it.

That's the whole node. If your workflow involves any "detect region, work on it, put it back" pattern - faces, logos, inpainting targets - this is a five-second replacement for a pile of manual crop math.

Categorybadger

Inputs (2)

NameTypeDefaultDescription
imageIMAGE
maskMASK

Outputs (3)

NameTypeDescription
cropped_imgIMAGE
XINT
YINT