Nodes/Best Resolution/Upscaled Crop/Pad (Best-Res)
ComfyUI Node

Upscaled Crop/Pad (Best-Res)

Exactly how much to crop or outpaint after an HD-fix upscale

By Lex-DRL·Created about a year ago·Updated 5 months ago· 15
Upscaled Crop/Pad (Best-Res)
    • upscale
    • do_crop
    • crop_width
    • crop_height
    • crop_x
    • crop_y
    • do_padding
    • pad_left
    • pad_top
    • pad_right
    • pad_bottom
    upscale1.50
    init_width1024
    init_height1024
    HD_width1536
    HD_height1536
    strategypad only
    align_x0.500
    align_y0.000

    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 (like ImageCrop) 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, or Pad 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.

    CategoryBest Resolution

    Inputs (8)

    NameTypeDefaultDescription
    upscaleFLOAT1.50Only used when <UpscaledCropPadStrategy.EXACT_UPSCALE: 'exact-upscale'> strategy selected.
    init_widthINT10241–9223372036854776000Width for original/initial image
    init_heightINT10241–9223372036854776000Height for original/initial image
    HD_widthINT15361–9223372036854776000Width for the (main/upscaled) HD-image
    HD_heightINT15361–9223372036854776000Height for the (main/upscaled) HD-image
    strategyCOMBOpad onlyIf 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_xFLOAT0.5000–1Where's the image pivot for horizontal alignment (which side stays in place during crop/out-paint): 0 - Left 0.5 - Center 1 - Right
    align_yFLOAT0.0000–1Where'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)

    NameTypeDescription
    upscaleFLOATThe actual uniform upscale value to do.
    do_cropBOOLEANWhether post-upscale crop needs to be done to get the specified resolution.
    crop_widthINTWidth for the post-upscale crop.
    crop_heightINTHeight for the post-upscale crop.
    crop_xINTX-offset for the post-upscale crop.
    crop_yINTY-offset for the post-upscale crop.
    do_paddingBOOLEANWhether post-upscale padding and out-paint need to be done to get the specified resolution.
    pad_leftINTLeft padding for the post-upscale out-paint.
    pad_topINTTop padding for the post-upscale out-paint.
    pad_rightINTRight padding for the post-upscale out-paint.
    pad_bottomINTBottom padding for the post-upscale out-paint.