-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathloadImageWithSubfolders.py
72 lines (60 loc) · 2.35 KB
/
loadImageWithSubfolders.py
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
import os
import folder_paths
import torch
import hashlib
from PIL import Image, ImageOps
from PIL.PngImagePlugin import PngInfo
import numpy as np
class LoadImage:
@classmethod
def INPUT_TYPES(s):
input_dir = folder_paths.get_input_directory()
exclude_folders = ["clipspace", "folder_to_exclude2"]
file_list = []
for root, dirs, files in os.walk(input_dir):
# Exclude specific folders
dirs[:] = [d for d in dirs if d not in exclude_folders]
for file in files:
file_path = os.path.relpath(os.path.join(root, file), start=input_dir)
file_path = file_path.replace("\\", "/") # so the filename is processed correctly in widgets.js
file_list.append(file_path)
return {"required":
{"image": (sorted(file_list), {"image_upload": True})},
}
CATEGORY = "image"
RETURN_TYPES = ("IMAGE", "MASK")
FUNCTION = "load_image"
def load_image(self, image):
image_path = folder_paths.get_annotated_filepath(image)
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,]
if 'A' in i.getbands():
mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0
mask = 1. - torch.from_numpy(mask)
else:
mask = torch.zeros((64,64), dtype=torch.float32, device="cpu")
return (image, mask.unsqueeze(0))
@classmethod
def IS_CHANGED(s, image):
image_path = folder_paths.get_annotated_filepath(image)
m = hashlib.sha256()
with open(image_path, 'rb') as f:
m.update(f.read())
return m.digest().hex()
@classmethod
def VALIDATE_INPUTS(s, image):
if not folder_paths.exists_annotated_filepath(image):
return "Invalid image file: {}".format(image)
return True
# A dictionary that contains all nodes you want to export with their names
# NOTE: names should be globally unique
NODE_CLASS_MAPPINGS = {
"LoadImagewithSubfolders": LoadImage
}
# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
"LoadImagewithSubfolders": "Load Image with Subfolders"
}