ComfyUI Node

TS Crop To Mask

Crop to the interesting part, process only that

By AlexYez·Created 2 years ago·Updated about 20 hours ago· 12
TS Crop To Mask
  • images
  • mask
  • cropped_images
  • cropped_masks
  • crop_data
  • width
  • height
padding64
divide_by32
max_resolution720
fixed_mask_frame_index0
interpolation_window_size0
force_gputrue
fixed_crop_sizefalse
fixed_width1024
fixed_height1024

The crop-and-restore pattern is one of the oldest tricks in the book: instead of running a heavy model over a whole 4K frame, crop to the region that actually matters - a face, an object - process just that at full detail, then paste it back. It's how you get an 8K face from a model that caps at 1K. TS Crop To Mask is the front half of that workflow: it crops a batch of images around a mask region, with padding, a resolution clamp, fixed aspect, and - the part most crop nodes skip - inter-frame smoothing for video.

You pair it with the pack's TS Restore From Crop, which pastes the processed crop back into the original frame with feathered seams. The README's framing is the use case: "running an upscaler or face restorer on a small ROI of a high-resolution image without burning VRAM on the full frame." The inpainting doc's crop-and-stitch discussion is the same idea from a different angle - process only the masked region, leave everything else untouched.

How it works

You feed it images and a mask. The node finds the bounding box of the mask's non-zero area, pads it by padding pixels, crops the images to that box, and rounds the crop up to a multiple of divide_by (default 32 - the VAE-friendly size). max_resolution caps the shorter side, so a huge region gets downscaled to fit - that's the VRAM guard.

The two interesting controls are the video ones:

  • fixed_mask_frame_index - default 0 means per-frame crops (the mask moves each frame). Set it to a 1-based frame index and the whole batch uses that one frame's mask as a fixed crop. That's how you keep a talking head stable - the crop doesn't chase the mask around.
  • interpolation_window_size - averages crop geometry over N neighboring frames, smoothing crop motion. 0 is off. For video, this is the difference between a crop that glides and one that judders.

There's also fixed_crop_size + fixed_width/fixed_height if you want every crop forced to a specific aspect ratio regardless of the mask.

The outputs

  • cropped_images and cropped_masks - the processed region, ready for your upscaler/restorer.
  • crop_data - the geometry blob you feed into TS Restore From Crop to paste the result back in the right place. Don't lose this wire; it's the whole "put it back" contract.
  • width / height - the crop's dimensions, for downstream sizing.

Installing it

Part of comfyui-timesaver (ComfyUI Manager → "Timesaver", or manual clone + pip install -r requirements.txt + restart). No model files - pure tensor cropping.

Gotchas

The trap is treating it as a still-image node when you're working with video: if the mask bounces around, the crop will too. That's what fixed_mask_frame_index and interpolation_window_size exist for - set a fixed frame and smooth the window, and the crop stops swimming. And divide_by is a round-up, not a round-down: the crop can come out a bit larger than the mask bounding box, which is intentional (VAE needs the alignment) but surprises people who expected pixel-exact bounding boxes. If you crop-then-restore with a model that changes resolution (some upscalers do), check the width/height outputs against your restore node's expectations before running a long batch.

CategoryTS/Image/Tiles

Inputs (11)

NameTypeDefaultDescription
imagesIMAGEBatch of images to crop around the mask region.
maskMASKMask that defines the region to crop. The bounding box of its non-zero area sets the crop.
paddingINT640–320Extra pixels added around the mask bounding box before cropping.
divide_byINT321–64Rounds the crop dimensions up to a multiple of this value (e.g. 32 for VAE-friendly sizes).
max_resolutionINT720320–2048Caps the shorter side of the crop. Larger crops are downscaled to this limit.
fixed_mask_frame_indexINT00–99991-based frame whose mask defines one fixed crop for the whole batch. 0 = per-frame crop.
interpolation_window_sizeINT00–100Smooths crop motion across frames by averaging over this many neighbouring frames. 0 = off.
force_gpuBOOLEANtrueRun the crop on GPU when CUDA is available; otherwise falls back to CPU.
fixed_crop_sizeBOOLEANfalseForce every crop to the fixed_width x fixed_height aspect ratio instead of following the mask.
fixed_widthINT102464–4096Target crop width when fixed_crop_size is enabled.
fixed_heightINT102464–4096Target crop height when fixed_crop_size is enabled.

Outputs (5)

NameTypeDescription
cropped_imagesIMAGE
cropped_masksMASK
crop_dataCROP_DATACrop geometry passed to TS Restore From Crop to paste the result back.
widthINTWidth of the cropped output.
heightINTHeight of the cropped output.