comfyanonymous / ComfyUI

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface.
https://www.comfy.org/
GNU General Public License v3.0
50.51k stars 5.31k forks source link

enhancement for upscale image #1591

Open Dex94 opened 11 months ago

Dex94 commented 11 months ago

I have no skill to create a node but I like to make a little contribute. I was thinking to a node to resize the image but mantaining proportions and selecting a target. Example I have an image of 512x600, I need a side of 1024. I have two tools: upscale image and putting 1024x1200 or upscale image by putting 2x. Both tools need a manual calc, first one needs an additional calc. With upscale models the calc is variable. Some models are upscaling 2x, some 4x... they aren't equal and the result is different. Writing a code that makes the calc automatically can makes the things easier. I don't know the logic of upscale nodes so I write two codes:

from PIL import Image

im = Image.open(#image to resize) w, h = im.size target #(w or h) # <- seeable in the node as input target_size=1024 # <- seeable in the node as input upscale_factor= (target_size/target) w1= w upscale_factor h1= h upscale_factor

if upscale image node needs x_upscale: (logic similar to 'upscale image by' node)

from PIL import Image

im = Image.open(#image to resize) x_upscale=4 # 4 is an example, select upscale multiplier of the model <- seeable in the node as input w, h = im.size target #(w or h) #<- seeable in the node as input target_size=1024 #<- seeable in the node as input upscale_factor= (target_size/target)/x_upscale #(pratically scale by input)

x_upscale select upscale multiplier of the model, example remacri is x4, in the input need to be set to 4 target_size is the measurement of the side taken in reference. example of width of 512, i select target w and target_size of 1024, automatically height is calculated as 1200. x_upscale is set manually, can be automatically putting a name list of known upscale models or calling a queue in the code putting a very little empty latent image, passing it to upscale model and calculating the size of the resulting image, then dividing the size with that one of the empty latent image. But it's complicated, it's easier to put it manually

jn-jairo commented 11 months ago

Good idea, I made a PR #1592 based on that idea, it is not exactly what you said but it allows proportional scale in an easy way.