customNode / wan_resizer.py
bjooo's picture
Upload 6 files
7aaa385 verified
Raw
History Blame Contribute Delete
2.4 kB
import math
import torch
import comfy.utils
class WanNativeResize_Dolphin:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
"target_model": (["480P (832x480)", "720P (1280x720)", "1080P (1920x1080)"], {"default": "720P (1280x720)"}),
"upscale_method": (["nearest-exact", "bilinear", "area", "bicubic", "lanczos"], {"default": "lanczos"}),
"alignment": ("INT", {"default": 16, "min": 8, "max": 64, "step": 8}),
}
}
RETURN_TYPES = ("IMAGE", "INT", "INT")
RETURN_NAMES = ("IMAGE", "width", "height")
FUNCTION = "resize_for_wan"
CATEGORY = "Dolphin Node/Wan Video"
def resize_for_wan(self, image, target_model, upscale_method, alignment):
# 1. ComfyUI ์ด๋ฏธ์ง€ ํ…์„œ์˜ ํ˜•ํƒœ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค: (Batch/Frames, Height, Width, Channels)
b, h, w, c = image.shape
# 2. ํƒ€๊ฒŸ ๋ชจ๋ธ์— ๋”ฐ๋ฅธ ์ด ํ”ฝ์…€ ๋ฉด์ (Area) ์„ธํŒ…
if "480P" in target_model:
target_area = 832 * 480
elif "720P" in target_model:
target_area = 1280 * 720
else:
target_area = 1920 * 1080
# 3. Byungjoo๋‹˜์˜ ์ˆ˜ํ•™ ๋กœ์ง (๋น„์œจ ์œ ์ง€ ๊ณ„์‚ฐ)
aspect_ratio = w / h
new_h = math.sqrt(target_area / aspect_ratio)
new_w = new_h * aspect_ratio
# 4. [ํ•ต์‹ฌ] VAE๊ฐ€ ์ข‹์•„ํ•˜๋Š” ๋ฐฐ์ˆ˜(Alignment, ๋ณดํ†ต 16)๋กœ ๋ฐ˜์˜ฌ๋ฆผ ์ฒ˜๋ฆฌ
new_w = int(round(new_w / alignment) * alignment)
new_h = int(round(new_h / alignment) * alignment)
# 5. [์ตœ์ ํ™”] ์ด๋ฏธ ๋ชฉํ‘œ ํ•ด์ƒ๋„์™€ ์ผ์น˜ํ•œ๋‹ค๋ฉด, ๋ฌด๊ฑฐ์šด ์—ฐ์‚ฐ ์—†์ด ์›๋ณธ ํ†ต๊ณผ (ํŒจ์Šค์Šค๋ฃจ)
if w == new_w and h == new_h:
return (image, new_w, new_h)
# 6. [ํ•ต์‹ฌ] ํ…์„œ ์ฐจ์› ๋ณ€๊ฒฝ: ComfyUI (B, H, W, C) -> ๋ฆฌ์‚ฌ์ด์ฆˆ ์—”์ง„์šฉ (B, C, H, W)
image = image.movedim(-1, 1)
# 7. ComfyUI ๋‚ด์žฅ ์—”์ง„์„ ์‚ฌ์šฉํ•ด ์ดˆ๊ณ ํ™”์งˆ ๋ฆฌ์‚ฌ์ด์ฆˆ (Lanczos ์ง€์›)
resized_image = comfy.utils.common_upscale(image, new_w, new_h, upscale_method, "disabled")
# 8. ํ…์„œ ์ฐจ์› ์›์ƒ ๋ณต๊ตฌ: (B, C, H, W) -> ComfyUI (B, H, W, C)
resized_image = resized_image.movedim(1, -1)
return (resized_image, new_w, new_h)