Files
jammyfu-ComfyUI_PaintingCod…/dynamic_image_input.py
T
jammyfu 1610fce3f4 Introduce DynamicImageCombiner class and update init.py for node integration.
This commit adds the DynamicImageCombiner class, enabling dynamic addition of multiple image inputs and outputting a list of connected images. The init.py file now includes this new node in NODE_CLASS_MAPPINGS and NODE_DISPLAY_NAME_MAPPINGS. A new JavaScript file has also been created to manage the node's behavior, enhancing image processing functionality.
引入DynamicImageCombiner类并更新__init__.py以实现节点集成。
此提交添加了DynamicImageCombiner类,可以动态地添加多个图像输入,并输出所有连接图像的列表。init.py文件现在在NODE_CLASS_MAPPINGS和NODE_DISPLAY_NAME_MAPPINGS中包含了这个新节点。此外,还创建了一个新的JavaScript文件来管理该节点的行为,增强了图像处理功能。
2024-12-25 22:23:01 +08:00

53 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
# Filename: dynamic_image_input.py
# Developer: jammyfu
# Category: 🎨Painting👓Coder/🖼️Image
import torch
class DynamicImageCombiner:
@classmethod
def INPUT_TYPES(s):
return {
"required": {}, # 不需要默认输入
"optional": {}
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("images",)
OUTPUT_IS_LIST = (True,) # 标记输出为列表
FUNCTION = "combine_images"
CATEGORY = "🎨Painting👓Coder/🖼️Image"
def combine_images(self, **image_inputs):
try:
# 收集所有非空图像
images = []
for i in range(1, len(image_inputs) + 1):
key = f"image_{i}"
if key in image_inputs and image_inputs[key] is not None:
if isinstance(image_inputs[key], torch.Tensor):
images.append(image_inputs[key])
# 如果没有图像,返回一个包含空白图像的列表
if not images:
empty_image = torch.zeros((1, 512, 512, 3))
return ([empty_image],)
# 直接返回图像列表
return (images,)
except Exception as e:
print(f"Error in DynamicImageCombiner: {str(e)}")
# 发生错误时返回包含空白图像的列表
empty_image = torch.zeros((1, 512, 512, 3))
return ([empty_image],)
# 添加到 ComfyUI 节点注册
NODE_CLASS_MAPPINGS = {
"DynamicImageCombiner": DynamicImageCombiner,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"DynamicImageCombiner": "Dynamic Image Input 🖼️",
}