mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2026-02-05 19:12:41 +08:00
- Implements Qwen image editing functionality with CLIP text encoding - Features intelligent scaling algorithm selection: - Uses 'area' method for downscaling to preserve details - Uses 'lanczos' method for upscaling for better quality - Supports optional VAE encoding for reference latents - Maintains aspect ratio with 'disabled' crop method - Scales images to target resolution (1024x1024 pixels) with 8-pixel alignment
65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
import node_helpers
|
|
import comfy.utils
|
|
import math
|
|
|
|
|
|
class TextEncodeQwenImageEdit:
|
|
@classmethod
|
|
def INPUT_TYPES(s):
|
|
return {"required": {
|
|
"clip": ("CLIP", ),
|
|
"prompt": ("STRING", {"multiline": True, "dynamicPrompts": True}),
|
|
},
|
|
"optional": {"vae": ("VAE", ),
|
|
"image": ("IMAGE", ),}}
|
|
|
|
RETURN_TYPES = ("CONDITIONING", "IMAGE", "LATENT")
|
|
FUNCTION = "encode"
|
|
|
|
CATEGORY = "advanced/conditioning"
|
|
|
|
def encode(self, clip, prompt, vae=None, image=None):
|
|
ref_latent = None
|
|
output_image = None
|
|
|
|
if image is None:
|
|
images = []
|
|
else:
|
|
samples = image.movedim(-1, 1)
|
|
total = int(1024 * 1024)
|
|
|
|
scale_by = math.sqrt(total / (samples.shape[3] * samples.shape[2]))
|
|
|
|
width = math.floor(samples.shape[3] * scale_by / 8) * 8
|
|
height = math.floor(samples.shape[2] * scale_by / 8) * 8
|
|
|
|
original_width = samples.shape[3]
|
|
original_height = samples.shape[2]
|
|
|
|
if width < original_width or height < original_height:
|
|
upscale_method = "area"
|
|
else:
|
|
upscale_method = "lanczos"
|
|
|
|
s = comfy.utils.common_upscale(samples, width, height, upscale_method, "disabled")
|
|
image = s.movedim(1, -1)
|
|
images = [image[:, :, :, :3]]
|
|
output_image = image[:, :, :, :3]
|
|
|
|
if vae is not None:
|
|
ref_latent = vae.encode(image[:, :, :, :3])
|
|
|
|
tokens = clip.tokenize(prompt, images=images)
|
|
conditioning = clip.encode_from_tokens_scheduled(tokens)
|
|
if ref_latent is not None:
|
|
conditioning = node_helpers.conditioning_set_values(conditioning, {"reference_latents": [ref_latent]}, append=True)
|
|
|
|
latent_output = {"samples": ref_latent} if ref_latent is not None else None
|
|
|
|
return (conditioning, output_image, latent_output)
|
|
|
|
|
|
NODE_CLASS_MAPPINGS = {
|
|
"TextEncodeQwenImageEdit": TextEncodeQwenImageEdit,
|
|
}
|