TinyTerra / ComfyUI_tinyterraNodes

A selection of nodes for Stable Diffusion ComfyUI
GNU General Public License v3.0
421 stars 45 forks source link

hiResFix - resize based on the loger side of the image and keep aspect #6

Closed hortom closed 1 year ago

hortom commented 1 year ago

Hi, is it possible to add another resize mode to this node? Something like "by longer side (keep aspect)"

It would require one parameter which is the target size of the longer_side, and the calculation would be something like this:

        if current_width > current_height:
            new_width, new_height = longer_side, int(current_height * longer_side / current_width)
        else:
            new_width, new_height = int(current_width * longer_side / current_height), longer_side

This would be useful because in the case you change the generated image aspect ratio and/or the size too, still the node would resize to the same dimension without any change of the parameters. I admit that maybe it wouldn't be beneficial to everyone but please consider adding this. :)

TinyTerra commented 1 year ago

Just pushed an update to the hiResFix node, hopefully that functions how you wanted it to. :)

hortom commented 1 year ago

Amazing! Thank you!

Looks like that the enforce_mul_of_64() function can receive a non-integer and return a non-integer. I pasted the whole error:

Error occurred when executing ttN hiresfixScale:

upsample_bilinear2d(): argument 'output_size' must be tuple of ints, but found element of type float at pos 2

File "e:\comfyUI\ComfyUI\execution.py", line 141, in recursive_execute
output_data, output_ui = get_output_data(obj, input_data_all)
File "e:\comfyUI\ComfyUI\execution.py", line 75, in get_output_data
return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True)
File "e:\comfyUI\ComfyUI\execution.py", line 68, in map_node_over_list
results.append(getattr(obj, func)(**slice_dict(input_data_all, i)))
File "E:\comfyUI\ComfyUI\custom_nodes\ComfyUI_tinyterraNodes\tinyterraNodes.py", line 1579, in upscale
s = comfy.utils.common_upscale(samples, width, height, rescale_method, crop)
File "e:\comfyUI\ComfyUI\comfy\utils.py", line 168, in common_upscale
return torch.nn.functional.interpolate(s, size=(height, width), mode=upscale_method)
File "e:\comfyUI\python_embeded\lib\site-packages\torch\nn\functional.py", line 3959, in interpolate
return torch._C._nn.upsample_bilinear2d(input, output_size, align_corners, scale_factors)

This fixed it - I added int(): (from line 1573)

                if orig_width > orig_height:
                    width, height = longer_side, enforce_mul_of_64(int(longer_side * orig_height / orig_width))
                else:
                    width, height = enforce_mul_of_64(int(longer_side * orig_width / orig_height)), longer_side

Solution 2 could be to convert the received value d in enforce_mul_of_64(d).

TinyTerra commented 1 year ago

Oh right, forgot to stage the function changes - pushed again