ImageMask_Constrain
Keep your image inside model-friendly resolution bounds
- image
- mask
- constrained_image
- constrained_mask
ImageMask_Constrain is the "make this image fit my pipeline's expectations" node. It rescales an input image so its dimensions land inside the bounds you declare - never larger than a maximum, never smaller than a minimum - and it does the exact same transform to the paired mask, keeping them locked together. That's the whole job, and it's a job you hit constantly once you start feeding arbitrary images into models that have preferred resolution ranges.
The mechanism is a two-pass resize with an optional crop. First it checks the maxes: if the image exceeds max_width or max_height, it scales down uniformly (preserving aspect ratio) so both fit. Then it checks the minimums: if the result is smaller than min_width or min_height, it scales back up until both are satisfied. If crop is off (the default), you get a resized image inside the box - possibly with one dimension larger than the minimum if the aspect ratio forces it. If crop is on, after the upscale it center-crops to exactly min_width × min_height, guaranteeing a precise target size at the cost of chopping edges. Image resizing is bicubic; the mask resizes nearest-neighbor (so hard mask edges stay hard) and is cropped to match.
The defaults give you a sense of the intended use: max 1024×1024, min 256×256. That's a "make sure it fits in the model's working resolution" window - feed a 4K texture in and it comes out at 1024; feed a 128px thumbnail in and it comes up to at least 256. If you're always thinking about latent dimensions and model-native resolutions, this is a softer, aspect-preserving alternative to hard-resizing to a fixed square.
The inputs that matter are the four ints: max_width / max_height (both default 1024) and min_width / min_height (both default 256). image is required; mask is optional, and if you leave it unconnected you still get a constrained_mask output - but it's a tensor of zeros at the constrained size, not your mask. Don't feed that downstream expecting real mask data. The two outputs, constrained_image and constrained_mask, are the resized pair.
Where you'd use it: as a front-door guard on image-input pipelines, or inline when a step (an upscaler, a detailer, a VAE) needs its input within a bounded resolution. It's the sort of node that quietly prevents a class of "why is my VRAM exploding" and "why is my output tiny" bugs in one place.
Installing it
cd ComfyUI/custom_nodes
git clone https://github.com/Ginolazy/ComfyPanel
# restart ComfyUI
or ComfyUI Manager → "ComfyPanel". No model files; the pack's standard torch/opencv/Pillow/kornia stack. If the crop looks off-center, that's by design - it center-crops, so an object sitting near the canvas edge will get its margin trimmed. Constrain to a min size with crop off unless you actually need the exact box.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| max_width | INT | 10241–4096 | — |
| max_height | INT | 10241–4096 | — |
| min_width | INT | 2561–4096 | — |
| min_height | INT | 2561–4096 | — |
| crop | BOOLEAN | false | — |
| maskopt | MASK | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| constrained_image | IMAGE | — |
| constrained_mask | MASK | — |