diff --git a/Mango_random_nodes.py b/Mango_random_nodes.py index 09e6a62..b576fac 100644 --- a/Mango_random_nodes.py +++ b/Mango_random_nodes.py @@ -5,6 +5,7 @@ from PIL import Image, ImageOps import numpy as np import cv2 import time +import re class RandomFilePathNode: @@ -94,7 +95,24 @@ class RandomImagePathNode: # 根据排序模式选择文件 elif sort_mode == "顺序循环": - files.sort(key=lambda x: os.path.basename(x).lower()) + def natural_sort_key(path): + # 提取文件名(不含路径和扩展名) + filename = os.path.splitext(os.path.basename(path))[0] + + # 将文件名分解为数字和非数字部分 + def convert(text): + # 如果是纯数字,转换为整数 + if text.isdigit(): + return int(text) + # 移除常见的分隔符 + text = text.replace('_', '').replace('-', '').replace(' ', '') + return text.lower() + + # 使用正则表达式分割,保留所有部分(包括非数字部分) + parts = re.split('([0-9]+)', filename) + return [convert(part) for part in parts if part] + + files.sort(key=natural_sort_key) path = files[self.current_index] self.current_index = (self.current_index + 1) % len(files) else: # 完全随机模式 @@ -258,14 +276,69 @@ class FrameGenerator: return iter(self.frames) +class RandomImageNode: + def __init__(self): + self.current_index = 0 + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "sort_mode": (["完全随机", "顺序循环"],), + "seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}), + }, + "optional": { + "image1": ("IMAGE",), + "image2": ("IMAGE",), + "image3": ("IMAGE",), + "image4": ("IMAGE",), + "image5": ("IMAGE",), + } + } + + RETURN_TYPES = ("IMAGE",) + FUNCTION = "random_image" + CATEGORY = "🥭 芒果节点/图片" + + @classmethod + def IS_CHANGED(cls, **kwargs): + return float("NaN") + + def random_image(self, sort_mode, seed, **kwargs): + # 收集所有输入的图片 + images = [] + for key, value in kwargs.items(): + if value is not None: # 只处理已连接的图片输入 + if isinstance(value, torch.Tensor): + images.extend([value[i:i+1] for i in range(value.shape[0])]) + + if not images: + raise ValueError("至少需要连接一个图片输入") + + if len(images) == 1: + return (images[0],) + + if sort_mode == "顺序循环": + selected_index = self.current_index % len(images) + self.current_index = (self.current_index + 1) % len(images) + selected_image = images[selected_index] + else: # 完全随机模式 + random.seed(seed) + selected_image = random.choice(images) + + return (selected_image,) + + NODE_CLASS_MAPPINGS = { "Random Video Path": RandomVideoPathNode, "Random Image Path": RandomImagePathNode, "Random File Path": RandomFilePathNode, + "Random Image": RandomImageNode, } NODE_DISPLAY_NAME_MAPPINGS = { "Random Video Path": "随机视频 🥭", - "Random Image Path": "随机图片 🥭", + "Random Image Path": "随机图片_路径 🥭", "Random File Path": "随机文件 🥭", + "Random Image": "随机图片 🥭", }