-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresolution_matrix.py
More file actions
147 lines (120 loc) · 4.42 KB
/
resolution_matrix.py
File metadata and controls
147 lines (120 loc) · 4.42 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
import comfy.utils
class PixelForge:
DESCRIPTION = "A ComfyUI node for selecting mathematically valid image resolutions filtered by aspect ratio, orientation, and megapixel limit."
"""
PixelForge Resolution Selector
A ComfyUI node for selecting mathematically valid resolutions
filtered by aspect ratio, divisibility, orientation, and megapixel limit.
"""
ASPECT_RATIOS = {
"1:1": (1, 1),
"3:2": (3, 2),
"4:3": (4, 3),
"16:9": (16, 9),
"16:10": (16, 10),
}
MP_BASE = 1024 * 1024 # 1 MP = 1,048,576 pixels
@classmethod
def INPUT_TYPES(cls):
# Generate ALL possible resolutions across all combinations
# JavaScript will filter them dynamically based on user selections
all_resolutions = cls._build_all_resolutions()
return {
"required": {
"aspect_ratio": (
list(cls.ASPECT_RATIOS.keys()),
{"default": "1:1"},
),
"orientation": (
["landscape", "portrait", "square"],
{"default": "square"},
),
"divisible_by": (
[16, 32, 64],
{"default": 16},
),
"max_megapixels": (
["1 MP", "2 MP", "4 MP", "6 MP", "8 MP", "12 MP", "16 MP"],
{"default": "1 MP"},
),
"resolution": (all_resolutions, {"default": "1024×1024"}),
}
}
@classmethod
def VALIDATE_INPUTS(cls, **kwargs):
# Accept any resolution string - JavaScript handles the filtering
return True
RETURN_TYPES = ("INT", "INT", "INT", "INT", "STRING", "FLOAT")
RETURN_NAMES = (
"width_px",
"height_px",
"ratio_w",
"ratio_h",
"orientation",
"total_megapixels",
)
FUNCTION = "forge"
CATEGORY = "PixelForge"
# ------------------------------------------------------------
@classmethod
def _build_all_resolutions(cls):
"""Generate all possible resolutions across all aspect ratios and settings."""
unique_resolutions = set()
# Iterate through all combinations
for aspect_name, (ratio_w, ratio_h) in cls.ASPECT_RATIOS.items():
for div in [16, 32, 64]:
for max_mp in [1, 2, 4, 6, 8, 12, 16]:
max_pixels = max_mp * cls.MP_BASE
k = div
while True:
w = ratio_w * k
h = ratio_h * k
total = w * h
if total > max_pixels:
break
# Add both landscape and portrait orientations
unique_resolutions.add(f"{w}×{h}")
unique_resolutions.add(f"{h}×{w}")
k += div
# Sort by total pixels for better UX
sorted_resolutions = sorted(
unique_resolutions,
key=lambda r: (
int(r.split("×")[0]) * int(r.split("×")[1]), # total pixels
int(r.split("×")[0]) # width
)
)
return sorted_resolutions if sorted_resolutions else ["1024×1024"]
def forge(
self,
aspect_ratio,
orientation,
divisible_by,
max_megapixels,
resolution,
):
ratio_w, ratio_h = self.ASPECT_RATIOS[aspect_ratio]
width, height = map(int, resolution.split("×"))
# Note: Orientation is already handled by the JavaScript selecting
# the appropriate dimension order, but we keep this for safety
if orientation == "portrait" and width > height:
width, height = height, width
elif orientation == "landscape" and width < height:
width, height = height, width
total_mp = (width * height) / self.MP_BASE
pbar = comfy.utils.ProgressBar(1)
pbar.update(1)
return (
width,
height,
ratio_w,
ratio_h,
orientation,
round(total_mp, 4),
)
NODE_CLASS_MAPPINGS = {
"PixelForge": PixelForge
}
NODE_DISPLAY_NAME_MAPPINGS = {
"PixelForge": "PixelForge · Resolution Selector"
}