-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
186 lines (155 loc) · 6.71 KB
/
Copy pathnode.py
File metadata and controls
186 lines (155 loc) · 6.71 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
import json
import torch
import numpy as np
from PIL import Image, ImageOps
from server import PromptServer
from aiohttp import web
# --- Hilfsfunktion: EXIF Sterne-Bewertung ---
def get_exif_rating(file_path):
try:
with Image.open(file_path) as img:
exif = img.getexif()
if exif:
rating = exif.get(18246)
if rating is not None:
if isinstance(rating, tuple): return int(rating[0])
return int(rating)
rating_pct = exif.get(18249)
if rating_pct is not None:
p = int(rating_pct)
if p >= 99: return 5
elif p >= 75: return 4
elif p >= 50: return 3
elif p >= 25: return 2
elif p > 0: return 1
except Exception:
pass
return 0
# --- API Endpunkte ---
@PromptServer.instance.routes.get("/custom_gallery/list")
async def list_images(request):
folder_path = request.rel_url.query.get("folder", "")
min_rating_str = request.rel_url.query.get("min_rating", "0")
page = int(request.rel_url.query.get("page", "1"))
limit = int(request.rel_url.query.get("limit", "100"))
try: min_rating = int(min_rating_str)
except: min_rating = 0
if not folder_path or not os.path.exists(folder_path):
return web.json_response({"error": "Ordner nicht gefunden", "images": [], "folders": [], "total_pages": 1})
valid_extensions = {".jpg", ".jpeg", ".png", ".webp", ".bmp"}
images = []
folders = []
try:
with os.scandir(folder_path) as it:
for entry in it:
if entry.is_dir():
folders.append(entry.name)
elif entry.is_file():
ext = os.path.splitext(entry.name)[1].lower()
if ext in valid_extensions:
if min_rating > 0:
rating = get_exif_rating(entry.path)
if rating < min_rating:
continue
images.append({"name": entry.name, "time": entry.stat().st_mtime})
images.sort(key=lambda x: x["time"], reverse=True)
folders.sort(key=str.lower)
total_images = len(images)
total_pages = max(1, (total_images + limit - 1) // limit)
start_idx = (page - 1) * limit
end_idx = start_idx + limit
paged_images = [img["name"] for img in images[start_idx:end_idx]]
return web.json_response({
"images": paged_images,
"folders": folders,
"total_pages": total_pages,
"current_page": page
})
except Exception as e:
return web.json_response({"error": str(e), "images": [], "folders": [], "total_pages": 1})
@PromptServer.instance.routes.get("/custom_gallery/view")
async def view_image(request):
folder_path = request.rel_url.query.get("folder", "")
filename = request.rel_url.query.get("file", "")
full_path = os.path.join(folder_path, filename)
if not os.path.exists(full_path): return web.Response(status=404)
return web.FileResponse(full_path)
# --- 1. Folder Gallery Loader Node ---
class FolderGalleryLoader:
# Interner Speicher für den Auto-Counter
_auto_index = {}
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"folder_path": ("STRING", {"default": "C:/Pfad/zu/Bildern"}),
"selection_mode": (["Multiple", "Single"], {"default": "Multiple"}),
"min_rating": ("INT", {"default": 0, "min": 0, "max": 5, "step": 1}),
"image_index": ("INT", {"default": 0, "min": 0, "max": 99999, "step": 1}),
# NEU: Der magische Schalter für automatisches Weiterblättern!
"auto_next": ("BOOLEAN", {"default": False}),
"preview_size": ("INT", {"default": 100, "min": 30, "max": 300, "step": 10}),
"show_folders": ("BOOLEAN", {"default": True}),
"selected_images": ("STRING", {"default": "[]"}),
},
# Wird benötigt, damit jede Node ihren eigenen Counter hat
"hidden": {"unique_id": "UNIQUE_ID"}
}
RETURN_TYPES = ("IMAGE", "MASK", "INT")
RETURN_NAMES = ("IMAGE", "MASK", "selected_count")
FUNCTION = "load_image"
CATEGORY = "image/custom"
# WICHTIG: Wenn auto_next an ist, erzwingen wir, dass ComfyUI die Node immer neu berechnet
@classmethod
def IS_CHANGED(s, auto_next, **kwargs):
if auto_next:
return float("nan")
return ""
def load_image(self, folder_path, selection_mode, min_rating, image_index, auto_next, preview_size, show_folders, selected_images, unique_id):
try:
images_list = json.loads(selected_images)
except:
images_list = []
count = len(images_list)
if count == 0:
empty_img = torch.zeros((1, 64, 64, 3))
empty_mask = torch.zeros((64, 64))
return (empty_img, empty_mask, 0)
# --- DIE NEUE LOGIK ---
if auto_next:
# 1. Aktuellen Index abrufen (Startet bei 0)
if unique_id not in self._auto_index:
self._auto_index[unique_id] = 0
idx = self._auto_index[unique_id]
# Sicherheitscheck falls Bilder abgewählt wurden
if idx >= count:
idx = 0
# 2. Schon mal für den NÄCHSTEN Durchlauf +1 hochzählen
next_idx = idx + 1
if next_idx >= count:
next_idx = 0 # Zurücksetzen, wenn am Ende
self._auto_index[unique_id] = next_idx
else:
# Wenn auto_next aus ist, verhält sich die Node normal mit image_index
idx = image_index % count
# --- Bild laden ---
selected_file = images_list[idx]
image_path = os.path.join(folder_path, selected_file)
i = Image.open(image_path)
i = ImageOps.exif_transpose(i)
image = i.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
mask = torch.zeros((64,64), dtype=torch.float32)
if 'A' in i.getbands():
mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0
mask = 1. - mask
mask = torch.from_numpy(mask)
return (image, mask, count)
NODE_CLASS_MAPPINGS = {
"FolderGalleryLoader": FolderGalleryLoader
}
NODE_DISPLAY_NAME_MAPPINGS = {
"FolderGalleryLoader": "Folder Gallery Loader"
}