11import node_helpers
22import comfy .utils
3-
4- PREFERRED_QWENIMAGE_RESOLUTIONS = [
5- (672 , 1568 ),
6- (688 , 1504 ),
7- (720 , 1456 ),
8- (752 , 1392 ),
9- (800 , 1328 ),
10- (832 , 1248 ),
11- (880 , 1184 ),
12- (944 , 1104 ),
13- (1024 , 1024 ),
14- (1104 , 944 ),
15- (1184 , 880 ),
16- (1248 , 832 ),
17- (1328 , 800 ),
18- (1392 , 752 ),
19- (1456 , 720 ),
20- (1504 , 688 ),
21- (1568 , 672 ),
22- ]
3+ import math
234
245
256class TextEncodeQwenImageEdit :
@@ -42,13 +23,17 @@ def encode(self, clip, prompt, vae=None, image=None):
4223 if image is None :
4324 images = []
4425 else :
45- images = [image ]
26+ samples = image .movedim (- 1 , 1 )
27+ total = int (1024 * 1024 )
28+
29+ scale_by = math .sqrt (total / (samples .shape [3 ] * samples .shape [2 ]))
30+ width = round (samples .shape [3 ] * scale_by )
31+ height = round (samples .shape [2 ] * scale_by )
32+
33+ s = comfy .utils .common_upscale (samples , width , height , "area" , "disabled" )
34+ image = s .movedim (1 , - 1 )
35+ images = [image [:, :, :, :3 ]]
4636 if vae is not None :
47- width = image .shape [2 ]
48- height = image .shape [1 ]
49- aspect_ratio = width / height
50- _ , width , height = min ((abs (aspect_ratio - w / h ), w , h ) for w , h in PREFERRED_QWENIMAGE_RESOLUTIONS )
51- image = comfy .utils .common_upscale (image .movedim (- 1 , 1 ), width , height , "lanczos" , "center" ).movedim (1 , - 1 )
5237 ref_latent = vae .encode (image [:, :, :, :3 ])
5338
5439 tokens = clip .tokenize (prompt , images = images )
0 commit comments