Upscaled Crop/Pad (Best-Res)
Exactly how much to crop or outpaint after an HD-fix upscale
- upscale
- do_crop
- crop_width
- crop_height
- crop_x
- crop_y
- do_padding
- pad_left
- pad_top
- pad_right
- pad_bottom
Here's the HD-fix trap that trips everyone eventually. Your initial image is, say, 1008x1008, and your second pass wants 1344x1344 - fine, that's exactly 1.333x. But your init is 1024x960 and your HD target is 1536x1344: the width scales by 1.5, the height by 1.4. If you upscale uniformly you get the wrong aspect ratio, and if you stretch you get a distorted image - and either way you've broken the "both passes stay step-clean" plan that made you reach for this pack in the first place.
Upscaled Crop/Pad (Best-Res) is the fix: feed it your init and HD dimensions and it tells you the exact uniform upscale to perform, plus precisely how many pixels to crop off or to outpaint on each side to hit the target frame. No guessing, no "close enough."
How it works
It checks whether the HD resolution can be reached by a single uniform scale of the init size. If yes, it hands back the precise factor (e.g. 1.3333) and do_crop/do_padding both false - clean win. If no, it works out the overflow or shortfall per axis and, depending on your strategy, produces either a crop (upscale to fill, trim the excess) or a pad (upscale to fit, outpaint the missing pixels) - or, with exact-upscale, a combination.
The align_x / align_y inputs decide the pivot: where the image "sticks" while the other side gets cropped or extended. 0/0 pins the bottom-left, 0.5 centers it, 1 pins the top/right. Default is centered horizontally, bottom-anchored vertically - a good fit for outpainting upward, which is the common case. When both crop and pad are needed, the outputs assume the order upscale → crop → pad.
The outputs, and how to wire them
upscale- the exact uniform factor to use for the upscale itself.do_crop,crop_width,crop_height,crop_x,crop_y- whether to crop, how big, and the offset. These feed a crop node (likeImageCrop) positioned between the upscale and the second KSampler.do_padding,pad_left,pad_top,pad_right,pad_bottom- whether to outpaint, and by how much on each side. These feed a pad node (ImagePad, orPad Image for Outpainting), after which you either mask the padded region or let a low-denoise second pass paint over it. The inpainting/outpainting docs in the KB cover the mask side; this node just tells you the numbers.
In a typical setup you won't type any of these - wire the init_width/init_height/HD_width/HD_height outputs straight from Best-Res (area+scale) into this node, and route its upscale back into the latent or image upscale. The area+scale node even tells you when you need it: if its status shows ⚠️ next to the upscale factor, the sizes aren't a uniform scale of each other, and that's exactly when this node earns its keep.
Install
cd ComfyUI/custom_nodes
git clone https://github.com/Lex-DRL/ComfyUI-BestResolution
or search "Best Resolution" in ComfyUI Manager, install, restart. Only frozendict as a dependency; no models. Like every node in this pack it only computes values - it won't crop or pad anything for you. That's the trade: you wire the numbers into your own crop/pad nodes, but you get exact, step-aware values instead of eyeballing them.
Inputs (8)
| Name | Type | Default | Description |
|---|---|---|---|
| upscale | FLOAT | 1.50 | Only used when <UpscaledCropPadStrategy.EXACT_UPSCALE: 'exact-upscale'> strategy selected. |
| init_width | INT | 10241–9223372036854776000 | Width for original/initial image |
| init_height | INT | 10241–9223372036854776000 | Height for original/initial image |
| HD_width | INT | 15361–9223372036854776000 | Width for the (main/upscaled) HD-image |
| HD_height | INT | 15361–9223372036854776000 | Height for the (main/upscaled) HD-image |
| strategy | COMBO | pad only | If the upscaled resolution can't be achieved by uniform scaling of the initial-res, how to tweak the image to get there: • pad only - upscale the image to fit it into the desired frame on one side, then add missing pixels on the other one for outpaint. • crop only - upscale the image to fill the entire frame, then crop extra pixels on one side. • nearest - automatically choose one of the above, to crop/pad the least number of pixels (by area). • exact-upscale - follow the provided upscale-value precisely. This is the only option that uses it. Also, it's the only one that might require both outpainting and crop. In case when both are required, the output values assume this order: upscale -> crop -> pad. |
| align_x | FLOAT | 0.5000–1 | Where's the image pivot for horizontal alignment (which side stays in place during crop/out-paint): 0 - Left 0.5 - Center 1 - Right |
| align_y | FLOAT | 0.0000–1 | Where's the image pivot for vertical alignment (which side stays in place during crop/out-paint): 0 - Bottom 0.5 - Center 1 - Top |
Outputs (11)
| Name | Type | Description |
|---|---|---|
| upscale | FLOAT | The actual uniform upscale value to do. |
| do_crop | BOOLEAN | Whether post-upscale crop needs to be done to get the specified resolution. |
| crop_width | INT | Width for the post-upscale crop. |
| crop_height | INT | Height for the post-upscale crop. |
| crop_x | INT | X-offset for the post-upscale crop. |
| crop_y | INT | Y-offset for the post-upscale crop. |
| do_padding | BOOLEAN | Whether post-upscale padding and out-paint need to be done to get the specified resolution. |
| pad_left | INT | Left padding for the post-upscale out-paint. |
| pad_top | INT | Top padding for the post-upscale out-paint. |
| pad_right | INT | Right padding for the post-upscale out-paint. |
| pad_bottom | INT | Bottom padding for the post-upscale out-paint. |