-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathload_image_list.py
More file actions
93 lines (73 loc) · 2.46 KB
/
load_image_list.py
File metadata and controls
93 lines (73 loc) · 2.46 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import torch
from PIL import Image
import numpy as np
def load_image(path: str) -> torch.Tensor:
"""
Load image from disk and convert to ComfyUI IMAGE tensor
Shape: [1, H, W, 3], float32 0..1
"""
img = Image.open(path).convert("RGB")
# Optimize: Direct to tensor -> float conversion (avoids intermediate float64 numpy array)
return torch.from_numpy(np.array(img)).float().div_(255.0)[None,]
class BatchLoadImageList:
CATEGORY = "FrameIO"
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"paths": ("STRING_LIST",),
},
"optional": {
"start": ("INT", {"default": 0, "min": 0}),
"end": ("INT", {"default": -1, "min": -1}),
"step": ("INT", {"default": 1, "min": 1}),
},
}
RETURN_TYPES = ("IMAGE", "INT")
FUNCTION = "execute"
DESCRIPTION = """
Loads images from a provided list of paths.
- Best used with `Batch Save Image Sequence` output.
- Supports slicing (start, end, step) for partial loading.
- Optimized with async loading and direct tensor conversion.
"""
def execute(
self,
paths,
start=0,
end=-1,
step=1,
):
if not paths:
raise RuntimeError("STRING_LIST is empty")
total = len(paths)
# Auto-detect end
if end < 0 or end > total:
end = total
if start >= end:
raise ValueError("Invalid frame range")
selected = paths[start:end:step]
if not selected:
raise RuntimeError("No frames selected after slicing")
from concurrent.futures import ThreadPoolExecutor
from comfy.utils import ProgressBar
pbar = ProgressBar(len(selected))
imgs = []
# Helper to check existence before loading to keep logic intact
def load_worker(path):
if not os.path.exists(path):
raise FileNotFoundError(f"Image does not exist: {path}")
return load_image(path)
with ThreadPoolExecutor() as executor:
for img in executor.map(load_worker, selected):
imgs.append(img)
pbar.update(1)
images = torch.cat(imgs, dim=0)
return (images, len(selected))
NODE_CLASS_MAPPINGS = {
"BatchLoadImageList": BatchLoadImageList
}
NODE_DISPLAY_NAME_MAPPINGS = {
"BatchLoadImageList": "Batch Load Image List (Advanced)"
}