-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnodes.py
More file actions
241 lines (188 loc) · 7.67 KB
/
Copy pathnodes.py
File metadata and controls
241 lines (188 loc) · 7.67 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import hashlib
import os
import numpy as np
import torch
from PIL import Image, ImageOps, ImageSequence
HEIC_EXTS = {".heic", ".heif"}
def _register_preview_route_if_possible() -> None:
"""Register /heic_preview once the PromptServer is available."""
try:
from aiohttp import web
from io import BytesIO
from server import PromptServer
instance = getattr(PromptServer, "instance", None)
if instance is None or not hasattr(instance, "routes"):
return
if getattr(instance, "_heic_preview_route_registered", False):
return
instance._heic_preview_route_registered = True
@instance.routes.get("/heic_preview")
async def heic_preview(request: web.Request):
filename = request.rel_url.query.get("filename")
if not filename:
return web.Response(status=400, text="filename is required")
folder_paths, _ = _get_comfy_modules()
if not folder_paths.exists_annotated_filepath(filename):
return web.Response(status=404, text="file not found")
image_path = folder_paths.get_annotated_filepath(filename)
_try_register_heif_opener()
try:
img = Image.open(image_path)
img = ImageOps.exif_transpose(img)
img = img.convert("RGBA")
except Exception as e:
return web.Response(status=500, text=f"failed to decode image: {e}")
bio = BytesIO()
img.save(bio, format="PNG")
return web.Response(body=bio.getvalue(), content_type="image/png")
except Exception:
return
def _get_comfy_modules():
try:
import folder_paths # provided by ComfyUI
import node_helpers # provided by ComfyUI
except Exception as e:
raise RuntimeError(
"This node must run inside a ComfyUI environment (missing folder_paths/node_helpers)."
) from e
return folder_paths, node_helpers
def _try_register_heif_opener() -> bool:
"""Register HEIF/HEIC opener for Pillow. Returns True if available."""
try:
from pillow_heif import register_heif_opener # type: ignore
register_heif_opener()
return True
except Exception:
return False
def _is_heic_path(name: str) -> bool:
_, ext = os.path.splitext(str(name))
return ext.lower() in HEIC_EXTS
def _list_heic_files_in_input_dir() -> list[str]:
folder_paths, _ = _get_comfy_modules()
input_dir = folder_paths.get_input_directory()
try:
files = [
f
for f in os.listdir(input_dir)
if os.path.isfile(os.path.join(input_dir, f))
]
except Exception:
return []
allowed_ext = {".heic", ".heif"}
out = []
for f in files:
_, ext = os.path.splitext(f)
if ext.lower() in allowed_ext:
out.append(f)
return sorted(out)
def _list_image_files_in_input_dir() -> list[str]:
"""Return all supported image files (PNG, JPG, WEBP, HEIC/HEIF)."""
folder_paths, _ = _get_comfy_modules()
input_dir = folder_paths.get_input_directory()
try:
files = [
f
for f in os.listdir(input_dir)
if os.path.isfile(os.path.join(input_dir, f))
]
except Exception:
return []
# Support all common image formats + HEIC
allowed_ext = {".png", ".jpg", ".jpeg", ".webp", ".heic", ".heif", ".bmp", ".gif"}
out = []
for f in files:
_, ext = os.path.splitext(f)
if ext.lower() in allowed_ext:
out.append(f)
return sorted(out)
class LoadImagePlusHEIC:
@classmethod
def INPUT_TYPES(cls):
_register_preview_route_if_possible()
files = _list_image_files_in_input_dir()
return {
"required": {
"image": (files, {"image_upload": True}),
},
}
CATEGORY = "image"
RETURN_TYPES = ("IMAGE", "MASK")
FUNCTION = "load_image"
def load_image(self, image):
if _is_heic_path(image) and not _try_register_heif_opener():
raise RuntimeError(
"HEIC/HEIF support is not available. Install dependency: pip install pillow-heif"
)
else:
_try_register_heif_opener()
folder_paths, node_helpers = _get_comfy_modules()
if not folder_paths.exists_annotated_filepath(image):
raise FileNotFoundError(f"Image not found in input path: {image}")
image_path = folder_paths.get_annotated_filepath(image)
try:
img = node_helpers.pillow(Image.open, image_path)
except Exception as e:
if _is_heic_path(image):
raise RuntimeError(
"Failed to open HEIC/HEIF image. Ensure pillow-heif is installed: pip install pillow-heif\n"
f"File: {image}\nError: {e}"
)
raise RuntimeError(f"Failed to open image: {image}\nError: {e}")
output_images = []
output_masks = []
w, h = None, None
excluded_formats = ["MPO"]
for i in ImageSequence.Iterator(img):
i = node_helpers.pillow(ImageOps.exif_transpose, i)
if i.mode == "I":
i = i.point(lambda i: i * (1 / 255))
image_rgb = i.convert("RGB")
if len(output_images) == 0:
w = image_rgb.size[0]
h = image_rgb.size[1]
if image_rgb.size[0] != w or image_rgb.size[1] != h:
continue
image_np = np.array(image_rgb).astype(np.float32) / 255.0
image_t = torch.from_numpy(image_np)[None,]
if "A" in i.getbands():
mask_np = np.array(i.getchannel("A")).astype(np.float32) / 255.0
mask_t = 1.0 - torch.from_numpy(mask_np)
elif i.mode == "P" and "transparency" in i.info:
mask_np = (
np.array(i.convert("RGBA").getchannel("A")).astype(np.float32) / 255.0
)
mask_t = 1.0 - torch.from_numpy(mask_np)
else:
mask_t = torch.zeros((64, 64), dtype=torch.float32, device="cpu")
output_images.append(image_t)
output_masks.append(mask_t.unsqueeze(0))
if not output_images:
raise RuntimeError(f"No frames could be decoded from image: {image}")
if len(output_images) > 1 and getattr(img, "format", None) not in excluded_formats:
output_image = torch.cat(output_images, dim=0)
output_mask = torch.cat(output_masks, dim=0)
else:
output_image = output_images[0]
output_mask = output_masks[0]
return (output_image, output_mask)
@classmethod
def IS_CHANGED(cls, image):
folder_paths, _ = _get_comfy_modules()
image_path = folder_paths.get_annotated_filepath(image)
m = hashlib.sha256()
with open(image_path, "rb") as f:
for chunk in iter(lambda: f.read(1024 * 1024), b""):
m.update(chunk)
return m.digest().hex()
@classmethod
def VALIDATE_INPUTS(cls, image):
folder_paths, _ = _get_comfy_modules()
if not folder_paths.exists_annotated_filepath(image):
return "Invalid image file: {}".format(image)
return True
NODE_CLASS_MAPPINGS = {
"LoadImagePlusHEIC": LoadImagePlusHEIC,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"LoadImagePlusHEIC": "Load Image (HEIC)",
}