PERF 合并读取文本文件, 读取图片同类型节点
This commit is contained in:
@@ -2,23 +2,29 @@ import os
|
||||
import uuid
|
||||
from io import BytesIO
|
||||
|
||||
import loguru
|
||||
import numpy as np
|
||||
import requests
|
||||
import torch
|
||||
from PIL import Image
|
||||
import folder_paths
|
||||
|
||||
|
||||
# 定义节点类
|
||||
class LoadNetImg:
|
||||
class LoadImgOptional:
|
||||
# 定义节点输入类型
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
input_dir = folder_paths.get_input_directory()
|
||||
files = [f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f))]
|
||||
files = folder_paths.filter_files_content_types(files, ["image"])
|
||||
return {
|
||||
"required": {
|
||||
"image_url": ("STRING", {
|
||||
"default": "https://example.com/sample.jpg",
|
||||
"multiline": False
|
||||
}),
|
||||
"image": (sorted(files), {"image_upload": True})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,28 +35,27 @@ class LoadNetImg:
|
||||
OUTPUT_NODE = False # 不允许该节点直接作为最终输出节点
|
||||
CATEGORY = "不忘科技-自定义节点🚩/图片" # 节点所属类别(在 ComfyUI 界面中分类)
|
||||
|
||||
def load_image_task(self, image_url):
|
||||
def load_image_task(self, image_url, image):
|
||||
try:
|
||||
if not image_url or not image_url.strip():
|
||||
raise ValueError("需要提供图片URL")
|
||||
|
||||
# 下载网络图片
|
||||
response = requests.get(image_url)
|
||||
response.raise_for_status() # 请求失败时抛出异常
|
||||
image = Image.open(BytesIO(response.content)).convert("RGB")
|
||||
if not image_url or len(image_url.strip()) == 0 or image_url == "https://example.com/sample.jpg":
|
||||
loguru.logger.info("读取本地文件")
|
||||
image_path = folder_paths.get_annotated_filepath(image)
|
||||
with open(image_path, "rb") as image_file:
|
||||
image = image_file.read()
|
||||
else:
|
||||
loguru.logger.info("读取线上文件")
|
||||
response = requests.get(image_url)
|
||||
response.raise_for_status()
|
||||
image = response.content
|
||||
image = Image.open(BytesIO(image)).convert("RGB")
|
||||
|
||||
# 按照官方格式转换图像数据
|
||||
# Convert to numpy array and normalize to 0-1
|
||||
image_array = np.array(image).astype(np.float32) / 255.0
|
||||
# Convert to torch tensor and add batch dimension
|
||||
image_tensor = torch.from_numpy(image_array)[None,]
|
||||
image_tensor = torch.from_numpy(image_array).unsqueeze(0)
|
||||
|
||||
return (image_tensor,) # 返回torch张量
|
||||
return (image_tensor,)
|
||||
except Exception as e:
|
||||
print(f"Error loading image: {e}")
|
||||
# 返回一个空的黑色图片作为错误处理
|
||||
empty_image = torch.zeros((1, 512, 512, 3), dtype=torch.float32)
|
||||
return (empty_image,)
|
||||
raise Exception(f"Error loading image: {e}")
|
||||
|
||||
|
||||
class SaveImagePath:
|
||||
|
||||
Reference in New Issue
Block a user