Nodes/ComfyUI Assistant Node/PVL Crop To Mask
ComfyUI Node

PVL Crop To Mask

Crop a subject out of a mask's bounding box, padding included

By pvlprk·Created about a year ago·Updated 8 months ago· 1
PVL Crop To Mask
  • image
  • mask
  • IMAGE
  • MASK
vertical_padding0.10
horizontal_padding0.10
apply_maskfalse

PVL Crop To Mask does one useful thing: it finds the bounding box of everything your mask covers, crops the image to that box, and hands you back both the crop and the cropped mask. That sounds trivial until you're mid-workflow and realize you've been doing it by hand. When you're feeding a face or a subject into a cloud API node, upscaling a single object, or building a character-consistency pipeline, cropping to the subject first is half the battle.

It's part of the ComfyUI Assistant Node pack from pvlprk - the same pack that wraps a dozen fal.ai generation APIs - but this node is pure local math. No API key, no network, no GPU beyond what's already running. It's one of the "everything else in the grab-bag" utilities that make the pack worth having even if you never touch the cloud stuff.

How it works

You feed it an image and a mask. It finds every pixel where the mask is above zero, takes the min/max in both directions to get a tight box around your subject, then expands that box by the two padding fractions before cutting. vertical_padding and horizontal_padding are fractions of the crop's own size (0.1 = 10% extra on each side), so the padding scales with the subject rather than being a fixed pixel count. The crop is clamped to the image edges, so a subject hugging the border won't error out.

There's one subtlety worth knowing: if the mask is empty, the node just returns the original image and mask untouched instead of failing. A "no subject found" run silently becomes a no-op, so pair it with something that checks the mask if that matters to you.

The inputs that matter

  • image - what to crop. Any standard ComfyUI IMAGE tensor.
  • mask - what defines the crop region. Typically the output of a segmentation or background-removal node.
  • vertical_padding / horizontal_padding - extra breathing room as a fraction, both default 0.1. Crank them up if your cutouts need context around the subject.
  • apply_mask - the one that changes output shape. Off (default) gives you a 3-channel RGB crop. On, the mask becomes the alpha channel and you get an RGBA image with the background transparent.

The outputs

  • IMAGE - the crop; 4 channels if apply_mask is on.
  • MASK - the mask, cropped to the same box, so everything stays pixel-aligned.

Wire both together downstream - the mask is exactly as useful as the image for compositing or further masking.

Installing it

Install the whole pack via ComfyUI Manager (search "ComfyUI Assistant Node") or:

cd ComfyUI/custom_nodes
git clone https://github.com/pvlprk/comfyui-pvl-api-nodes

Then restart ComfyUI. The pack's requirements.txt installs a long list of dependencies, but this node itself only needs torch, which you already have.

Where people get burned

The classic mistake is expecting the padding to be absolute pixels; it's relative to the crop size, so a big subject gets a bigger pad. If you need a fixed margin, you'll want to resize or pad afterward. And remember the apply_mask RGBA output - if you feed a 4-channel image into a node that expects RGB, you'll get a confusing error that has nothing to do with this node.

CategoryPVL Nodes/Image

Inputs (5)

NameTypeDefaultDescription
imageIMAGE
maskMASK
vertical_paddingFLOAT0.100–1
horizontal_paddingFLOAT0.100–1
apply_maskBOOLEANfalse

Outputs (2)

NameTypeDescription
IMAGEIMAGE
MASKMASK