NH Smart Ratio Image Resize
Let the model's preset table pick your crop for you
- image
- image
- width
- height
- info
- source_aspect_ratio
- target_aspect_ratio
- selected_preset
- resize_data
Every img2img workflow has the same awkward pre-step: your source image is 1342×890 or some other uncooperative size, and your model wants 1024×1024 or 1216×832 - one of the sizes it actually trains on. NH Smart Ratio Image Resize does the deciding for you. You tell it the model family and a target resolution level, and it picks the closest matching aspect preset from that model's own table, then resizes your image into it.
You feed it an image plus model_family (FLUX.2 Klein 4B/9B, FLUX.2 Dev, Qwen-Image, Z-Image) and resolution_level (1 MP through 12 MP). The node computes your source image's aspect ratio, finds the preset at that resolution level whose ratio is closest, and resizes to it. That's the "smart" part: it doesn't force your 2:3 image into a 1:1 box and hope; it lands on the closest ratio the model actually supports.
The three resize modes
- cover_crop (default) - scales to fill the target and crops the overflow. Nothing gets distorted, edges get cut. Best when you don't care about the image edges (most inpainting and img2img cases).
- contain_pad - scales to fit and pads the leftover space with
pad_color_hex(default white). Nothing gets cropped; you get letterbox bars. This is the mode that records the content region for later removal. - stretch - distorts the image to exactly the target dimensions. Fast, usually ugly; it's there for the cases where aspect literally doesn't matter.
resize_method (default lanczos) picks the interpolation, and pad_color_hex sets the pad fill. The outputs are generous: image, width, height, info (a summary that tells you the source aspect, the closest-ratio delta, and what it did), source_aspect_ratio, target_aspect_ratio, selected_preset, and resize_data.
The resize_data hook
resize_data is the node's party trick. In contain_pad mode it carries the content region, the padding, and the scale factor. Feed it into NH Remove Resize Padding at the end of your pipeline and that node crops the padded border back off - or restores your original input size - so the letterboxing never shows up in the final image. Run the whole thing as Load Image → NH Smart Ratio Image Resize → (your pipeline) → NH Remove Resize Padding and the padding round-trips cleanly. In cover_crop or stretch modes, resize_data is still emitted but there's less to undo.
Install
It's in NH-Nodes:
- ComfyUI Manager → search NH-Nodes → install → restart ComfyUI. Or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/jetthuangai/NH-Nodes.git
cd NH-Nodes
pip install -r requirements.txt
Then restart ComfyUI.
No model downloads - the preset table is embedded in the pack.
Common issues
The classic surprise is the white padding bars appearing in the output - that's contain_pad doing its job, and the fix is wiring resize_data into Remove Resize Padding downstream. If the crop in cover_crop mode clips something you care about, bump resolution_level so the target is bigger relative to the source, or switch to contain_pad. And remember: if you want to pick the ratio yourself instead of letting the node choose, that's NH Ratio Preset Image Resize, its manual sibling.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| model_family | COMBO | FLUX.2 Klein 9B | 5 options: FLUX.2 Klein 4B, FLUX.2 Klein 9B, FLUX.2 Dev, Qwen-Image, Z-Image |
| resolution_level | COMBO | 1 MP | 12 options: 1 MP, 2 MP, 3 MP, 4 MP, 5 MP, 6 MP, +6 |
| resize_mode | COMBO | cover_crop | 3 options: cover_crop, contain_pad, stretch |
| resize_method | COMBO | lanczos | 8 options: lanczos, bicubic, bilinear, nearest, nearest-exact, area, +2 |
| pad_color_hex | STRING | #FFFFFF | — |
Outputs (8)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | — |
| width | INT | — |
| height | INT | — |
| info | STRING | — |
| source_aspect_ratio | FLOAT | — |
| target_aspect_ratio | FLOAT | — |
| selected_preset | STRING | — |
| resize_data | NH_RESIZE_DATA | — |