-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.py
163 lines (122 loc) · 4.07 KB
/
nodes.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
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
import base64
from PIL import Image
import torch
import numpy as np
import io
import folder_paths
class MyxBase64ImageInput:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"base64_image": ("STRING", {"multiline": False, "default": ""}),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "process_input"
CATEGORY = "Myx-Tools"
def process_input(self, base64_image):
if base64_image:
image_bytes = base64.b64decode(base64_image)
# Open the image from bytes
image = Image.open(io.BytesIO(image_bytes))
image = image.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
return (image,)
class MyxBase64ImageOutput:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"images": ("IMAGE",),
"field_tag": ("STRING", {
"multiline": False,
"default": "base64_output"
}),
},
}
RETURN_TYPES = ()
FUNCTION = "process_image_output"
OUTPUT_NODE = True
CATEGORY = "Myx-Tools"
def process_image(self, image):
i = 255.0 * image.cpu().numpy()
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
buffered = io.BytesIO()
img.save(buffered, optimize=False, format="png", compress_level=4)
base64_image = base64.b64encode(buffered.getvalue()).decode()
return base64_image
def process_image_output(self, images: list[torch.Tensor], field_tag):
if hasattr(images, "__len__"):
return {"ui": {str(field_tag): [self.process_image(image) for image in images]}}
else:
return {"ui": {str(field_tag): [self.process_image(images)]}}
class MyxBase64AudioOutput:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"audio": ("AUDIO",),
"field_tag": ("STRING", {
"multiline": False,
"default": "base64_output"
}),
},
}
RETURN_TYPES = ()
FUNCTION = "process_audio_output"
OUTPUT_NODE = True
CATEGORY = "Myx-Tools"
def process_audio(self, audio):
with open(folder_paths.get_output_directory() + "/" + audio["filename"], 'rb') as binary_file:
binary_file_data = binary_file.read()
base64_encoded_data = base64.b64encode(binary_file_data)
base64_output = base64_encoded_data.decode('utf-8')
return base64_output
def process_audio_output(self, audio, field_tag):
return {"ui": {str(field_tag): [self.process_audio(audio)]}}
class MyxAlwaysEqualProxy(str):
def __eq__(self, _):
return True
def __ne__(self, _):
return False
class MyxBase64Output:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"ANY": (MyxAlwaysEqualProxy("*"),),
"field_tag": ("STRING", {
"multiline": False,
"default": "base64_output"
}),
},
}
RETURN_TYPES = ()
FUNCTION = "process_output"
OUTPUT_NODE = True
CATEGORY = "Myx-Tools"
def process_output(self, ANY, field_tag):
return {"ui": {str(field_tag): ANY}}
NODE_CLASS_MAPPINGS = {
"MyxBase64ImageInput": MyxBase64ImageInput,
"MyxBase64ImageOutput": MyxBase64ImageOutput,
"MyxBase64AudioOutput": MyxBase64AudioOutput,
"MyxBase64Output": MyxBase64Output
}
# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
"MyxBase64ImageInput": "MyxBase64ImageInput Node",
"MyxBase64ImageOutput": "MyxBase64ImageOutput Node",
"MyxBase64AudioOutput": "MyxBase64AudioOutput Node",
"MyxBase64Output": "MyxBase64Output Node"
}