File size: 2,399 Bytes
7aaa385 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | 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) |