forked from jtydhr88/ComfyUI-ImageMagick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_magick_node.py
223 lines (182 loc) · 6.77 KB
/
image_magick_node.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
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
import torch
from PIL import Image, ImageOps, ImageSequence
import numpy as np
import torch
import torchvision
import torchvision.transforms as T
from PIL import Image
import subprocess
import os
import datetime
import folder_paths
comfy_path = folder_paths.get_folder_paths("custom_nodes")[0]
image_magick_path = f'{comfy_path}ComfyUI-ImageMagick'
old_comfy_path = os.path.dirname(folder_paths.__file__)
print(f"[EmProps] Old comfy path: {old_comfy_path}")
print(f"[EmProps] Image magick path: {image_magick_path}")
import uuid
class ImageMagick:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image1": ("IMAGE",),
"param1": ("STRING", {
"multiline": False,
"default": ""
}),
"param2": ("STRING", {
"multiline": False,
"default": ""
}),
"param3": ("STRING", {
"multiline": False,
"default": ""
}),
"param4": ("STRING", {
"multiline": False,
"default": ""
}),
"param5": ("STRING", {
"multiline": False,
"default": ""
}),
"param6": ("STRING", {
"multiline": False,
"default": ""
}),
"param7": ("STRING", {
"multiline": False,
"default": ""
}),
"param8": ("STRING", {
"multiline": False,
"default": ""
}),
"param9": ("STRING", {
"multiline": False,
"default": ""
}),
"param10": ("STRING", {
"multiline": False,
"default": ""
}),
"param11": ("STRING", {
"multiline": False,
"default": ""
}),
"param12": ("STRING", {
"multiline": False,
"default": ""
}),
"param13": ("STRING", {
"multiline": False,
"default": ""
}),
"param14": ("STRING", {
"multiline": False,
"default": ""
}),
"param15": ("STRING", {
"multiline": False,
"default": ""
}),
"param16": ("STRING", {
"multiline": False,
"default": ""
}),
"param17": ("STRING", {
"multiline": False,
"default": ""
}),
"param18": ("STRING", {
"multiline": False,
"default": ""
}),
"param19": ("STRING", {
"multiline": False,
"default": ""
}),
"param20": ("STRING", {
"multiline": False,
"default": ""
}),
},
"optional": {
"image2": ("IMAGE",),
"image3": ("IMAGE",),
}
}
RETURN_TYPES = ("IMAGE", "STRING")
RETURN_NAMES = ("image", "image_path")
FUNCTION = "execute"
#OUTPUT_NODE = False
CATEGORY = "ImageMagick"
def save_image(self, images):
img_full_path = None
for (batch_number, ii) in enumerate(images):
i = 255. * ii.cpu().numpy()
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
now = datetime.datetime.now()
timestamp = now.strftime("%Y%m%d_%H%M%S")
img_file_name = f'ImageMagick_{timestamp}.png'
tmp_path = f'{image_magick_path}/tmp'
if not os.path.exists(tmp_path):
os.makedirs(tmp_path)
img_full_path = f'{tmp_path}/{img_file_name}'
img_full_path = os.path.normpath(img_full_path)
if os.path.exists(img_full_path):
random_suffix = uuid.uuid4().hex
img_file_name = f'ImageMagick_{timestamp}_{random_suffix}.png'
img_full_path = os.path.join(tmp_path, img_file_name)
img.save(img_full_path)
return img_full_path
def read_image_from_path(self, image_path):
img = Image.open(image_path)
image = None
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")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
return image
def execute(self, image1, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10,
param11, param12, param13, param14, param15, param16, param17, param18, param19, param20, image2=None, image3=None):
image1_store_path = self.save_image(image1)
image2_store_path = None
if not image2 is None:
image2_store_path = self.save_image(image2)
image3_store_path = None
if not image3 is None:
image3_store_path = self.save_image(image3)
now = datetime.datetime.now()
timestamp = now.strftime("%Y%m%d_%H%M%S")
img_file_name = f'ImageMagick_{timestamp}.png'
out_path = f'{image_magick_path}/output'
if not os.path.exists(out_path):
os.makedirs(out_path)
image_out_path = f'{out_path}/{img_file_name}'
image_out_path = os.path.normpath(image_out_path)
params = ['magick', image1_store_path, image2_store_path, image3_store_path,
param1, param2, param3, param4, param5, param6, param7, param8, param9, param10,
param11, param12, param13, param14, param15, param16, param17, param18, param19, param20,
image_out_path]
filtered_params = [param for param in params if param not in ['', None]]
print(filtered_params)
result = subprocess.run(filtered_params)
if result.returncode == 0:
print("Image processed successfully.")
else:
print("Error processing image.")
return (self.read_image_from_path(image_out_path), image_out_path)
NODE_CLASS_MAPPINGS = {
"ImageMagick": ImageMagick
}
# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
"ImageMagick": "ImageMagick"
}