-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathload_images_from_folder.py
96 lines (76 loc) · 3.89 KB
/
load_images_from_folder.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import numpy as np
from PIL import Image, ImageSequence, ImageOps
import torch
class LoadImagesFromSelectedFolder:
@classmethod
def INPUT_TYPES(cls):
# Get the directory where this script is located
script_dir = os.path.dirname(os.path.abspath(__file__))
comfyui_root = os.path.abspath(os.path.join(script_dir, '..', '..'))
output_dir = os.path.join(comfyui_root, 'output')
def count_images(folder_path):
# Count the number of image files in the folder
return len([f for f in os.listdir(folder_path) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))])
folders = []
for root, dirs, files in os.walk(output_dir):
rel_path = os.path.relpath(root, output_dir)
if rel_path == '.':
continue
image_count = count_images(root)
if image_count > 0:
folder_name = f"{rel_path} ({image_count} images)"
folders.append((folder_name, rel_path))
# Sort folders alphabetically, case-insensitive
folders.sort(key=lambda x: x[0].lower())
return {
"required": {
"selected_folder": ([folder[0] for folder in folders],),
}
}
RETURN_TYPES = ("IMAGE", "IMAGE", "IMAGE", "IMAGE")
RETURN_NAMES = ("Images resolution 1", "Images resolution 2", "Images resolution 3", "Images resolution 4")
FUNCTION = "load_images_from_selected_folder"
CATEGORY = "Bjornulf"
def load_images_from_selected_folder(self, selected_folder):
script_dir = os.path.dirname(os.path.abspath(__file__))
comfyui_root = os.path.abspath(os.path.join(script_dir, '..', '..'))
output_dir = os.path.join(comfyui_root, 'output')
folder_path = os.path.join(output_dir, selected_folder.split(" (")[0])
images_by_resolution = {}
# Check if the folder exists and contains images
if not os.path.exists(folder_path):
print(f"Folder {folder_path} does not exist.")
return (None, None, None)
image_files = [f for f in os.listdir(folder_path) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))]
if not image_files:
print(f"No images found in folder {folder_path}.")
return (None, None, None)
for image_file in image_files:
image_path = os.path.join(folder_path, image_file)
img = Image.open(image_path)
for i in ImageSequence.Iterator(img):
i = ImageOps.exif_transpose(i)
if i.mode == 'I':
i = i.point(lambda i: i * (1 / 255))
image = i.convert("RGB")
resolution = image.size
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
if resolution not in images_by_resolution:
images_by_resolution[resolution] = []
images_by_resolution[resolution].append(image)
# Sort resolutions by total pixel count (width * height)
sorted_resolutions = sorted(images_by_resolution.keys(), key=lambda r: r[0] * r[1], reverse=True)
outputs = []
for i in range(4): # Return up to 4 different resolutions
if i < len(sorted_resolutions):
resolution = sorted_resolutions[i]
output_image = torch.cat(images_by_resolution[resolution], dim=0)
outputs.append(output_image)
else:
# Create a placeholder tensor filled with 11111111111111111111111
H, W, C = 64, 64, 3
placeholder_image = torch.ones((1, H, W, C), dtype=torch.float32)
outputs.append(placeholder_image)
return tuple(outputs)