-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdrawing_assistant_nodes.py
More file actions
415 lines (302 loc) · 16.9 KB
/
drawing_assistant_nodes.py
File metadata and controls
415 lines (302 loc) · 16.9 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import cv2
import numpy as np
import torch
from .utils import new_bg, darken_image, new_ink, new_ink2, contrast_image, getColor, convert_to_uint8, remove_noise_from_sketch
class Sketch_Assistant_grayScale():
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"line_strength": ("FLOAT", {"default": 1, "min": 1, "max": 10, "step": 0.1}),
"shading_effect": ("INT", {"default": 41, "min": 5, "max": 105, "step": 2}),
"details": ("INT", {"default": 225, "min": 50, "max": 255, "step": 1}),
"smoothness": ("FLOAT", {"default": 2.0, "min": 1.0, "max": 5.0, "step": 0.1}),
"contrast": ("FLOAT", {"default": 100.0, "min": 1.0, "max": 150.0, "step": 1.0}),
"noise_removal": ("INT", {"default": 0, "min": 0, "max": 5, "step": 1}),
}
}
RETURN_TYPES = ("IMAGE", "IMAGE")
RETURN_NAMES = ("sketch", "grayscale image")
FUNCTION = "process"
CATEGORY = "image"
def process(self, image, line_strength, shading_effect, details, smoothness, contrast, noise_removal):
imgNo = image.shape[0]
if imgNo == 1:
print("1 image received for conversion to grayscale sketch")
else:
print(f"{imgNo} images received for conversion to grayscale sketch")
images = []
bandwImages = []
for img in image:
sketch, bandwI = processImg2SketchGrayScale(img, line_strength, shading_effect, details, smoothness, contrast, noise_removal)
images.append(sketch)
bandwImages.append(bandwI)
images = torch.cat(images, dim=0)
bandwImages = torch.cat(bandwImages, dim=0)
print("Conversion complete!")
return (images, bandwImages)
class Sketch_Assistant():
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"artist": (["1", "2"],),
"line_strength": ("FLOAT", {"default": 2, "min": 1, "max": 10, "step": 0.1}),
"shading_effect": ("INT", {"default": 41, "min": 5, "max": 105, "step": 2}),
"line_color": (["black", "white", "red", "lime", "blue", "yellow", "cyan", "magenta", "silver", "gray", "maroon", "olive", "green", "purple", "teal", "navy"],),
"details": ("INT", {"default": 240, "min": 50, "max": 255, "step": 1}),
"smoothness": ("FLOAT", {"default": 1.0, "min": 1.0, "max": 5.0, "step": 0.1}),
"enable_bg_color_change": ("BOOLEAN", { "default": False }),
"bg_color": (["white", "black", "red", "lime", "blue", "yellow", "cyan", "magenta", "silver", "gray", "maroon", "olive", "green", "purple", "teal", "navy"],),
"bg_light": ("FLOAT", {"default": 10, "min": 1, "max": 100, "step": 1}),
"noise_removal": ("INT", {"default": 0, "min": 0, "max": 5, "step": 1}),
}
}
RETURN_TYPES = ("IMAGE", "IMAGE")
RETURN_NAMES = ("sketch", "grayscale image")
FUNCTION = "process"
CATEGORY = "image"
def process(self, image, artist, line_strength, shading_effect, line_color, details, smoothness, enable_bg_color_change, bg_color, bg_light, noise_removal):
imgNo = image.shape[0]
if imgNo == 1:
print("1 image received for conversion to sketch")
else:
print(f"{imgNo} images received for conversion to sketch")
images = []
bandwImages = []
for img in image:
sketch, bandwI = processImg2Sketch(img, artist, line_strength, shading_effect, line_color, details, smoothness, enable_bg_color_change, bg_color, bg_light, noise_removal)
images.append(sketch)
bandwImages.append(bandwI)
images = torch.cat(images, dim=0)
bandwImages = torch.cat(bandwImages, dim=0)
print("Conversion complete!")
return (images, bandwImages)
class LineArt_Assistant():
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"line_thickness": ("INT", {"default": 11, "min": 3, "max": 81, "step": 2}),
"Clean_up": ("INT", {"default": 7, "min": 1, "max": 49, "step": 2}),
"deep_Clean_up": ("INT", {"default": 2, "min": 0, "max": 5, "step": 1}),
"line_color": (["black", "gray", "white", "lime", "blue", "yellow", "cyan", "magenta", "silver", "red", "maroon", "olive", "green", "purple", "teal", "navy"],),
"color_strength": ("FLOAT", {"default": 10, "min": 1, "max": 10, "step": 0.1}),
"details": ("INT", {"default": 9, "min": 1, "max": 9, "step": 1}),
"smoothness": ("FLOAT", {"default": 1.0, "min": 1.0, "max": 5.0, "step": 0.1}),
"bg_color": (["white", "black", "red", "lime", "blue", "yellow", "cyan", "magenta", "silver", "gray", "maroon", "olive", "green", "purple", "teal", "navy"],),
"bg_light": ("FLOAT", {"default": 10, "min": 1, "max": 100, "step": 1}),
},
# "optional": {
# "from_LineArt_Preprocessor": ("IMAGE",),
# },
}
RETURN_TYPES = ("IMAGE", "IMAGE")
RETURN_NAMES = ("lineArt", "grayscale image")
FUNCTION = "process"
CATEGORY = "image"
def process(self, image, line_thickness, Clean_up, deep_Clean_up, line_color, color_strength, details, smoothness, bg_color, bg_light):
images = []
bandwImages = []
# if use_preprocessors:
# imgNo = from_LineArt_Preprocessor.shape[0]
# if imgNo == 1:
# print("1 image received from preprocessor")
# else:
# print(f"{imgNo} images received from preprocessor")
# for img in from_LineArt_Preprocessor:
# sketch, bandwI = processLineArt(img, 240, deep_Clean_up, line_color, color_strength, bg_color, bg_light)
# images.append(sketch)
# bandwImages.append(bandwI)
# else:
imgNo = image.shape[0]
if imgNo == 1:
print("1 image received for conversion to lineart")
else:
print(f"{imgNo} images received for conversion to lineart")
for img in image:
sketch, bandwI = processImg2LineArt(img, line_thickness, Clean_up, deep_Clean_up, line_color, color_strength, details, smoothness, bg_color, bg_light)
images.append(sketch)
bandwImages.append(bandwI)
images = torch.cat(images, dim=0)
bandwImages = torch.cat(bandwImages, dim=0)
print("Conversion complete!")
return (images, bandwImages)
class LineArt_Assistant_2():
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"lineArt": ("IMAGE",),
"details": ("INT", {"default": 240, "min": 25, "max": 255, "step": 1}),
"clean_up": ("INT", {"default": 0, "min": 0, "max": 5, "step": 1}),
"line_color": (["black", "gray", "white", "lime", "blue", "yellow", "cyan", "magenta", "silver", "red", "maroon", "olive", "green", "purple", "teal", "navy"],),
"color_strength": ("FLOAT", {"default": 8, "min": 1, "max": 10, "step": 0.1}),
"bg_color": (["white", "black", "red", "lime", "blue", "yellow", "cyan", "magenta", "silver", "gray", "maroon", "olive", "green", "purple", "teal", "navy"],),
"bg_light": ("FLOAT", {"default": 10, "min": 1, "max": 100, "step": 1}),
"invert": ("BOOLEAN", { "default": True }),
"invert_default": ("BOOLEAN", { "default": True }),
},
}
RETURN_TYPES = ("IMAGE", "IMAGE")
RETURN_NAMES = ("lineArt", "default")
FUNCTION = "process"
CATEGORY = "image"
def process(self, lineArt, clean_up, line_color, color_strength, details, bg_color, bg_light, invert, invert_default):
images = []
bandwImages = []
imgNo = lineArt.shape[0]
if imgNo == 1:
print("1 image received")
else:
print(f"{imgNo} images")
for img in lineArt:
sketch, bandwI = processLineArt(img, details, clean_up, line_color, color_strength, bg_color, bg_light, invert, invert_default)
images.append(sketch)
bandwImages.append(bandwI)
images = torch.cat(images, dim=0)
bandwImages = torch.cat(bandwImages, dim=0)
return (images, bandwImages)
def processImg2SketchGrayScale(image, line_strength, shading_effect, details, smoothness, contrast, noise_removal):
if image is None:
raise ValueError("Input image is required")
if isinstance(image, torch.Tensor):
image = image.squeeze(0).cpu().numpy()
image = convert_to_uint8(image)
if image.ndim not in [2, 3]:
raise ValueError("Input image must be 2D or 3D numpy array")
img_height, img_width = image.shape[:2]
# print(f"The width of the image is: {img_width}, and the height is: {img_height}")
image = cv2.resize(image, (int(img_width * smoothness), int(img_height * smoothness)), interpolation=cv2.INTER_AREA)
img_height2, img_width2 = image.shape[:2]
line_strength = 11 - line_strength
line_intensity = line_strength / 10
contrast = contrast/100
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurEffect_s = shading_effect
blurred_image_s = cv2.GaussianBlur(gray_image, (blurEffect_s, blurEffect_s), 0)
sketch = cv2.divide(gray_image, blurred_image_s, scale=256.0)
sketch = cv2.resize(sketch, (int(img_width2 * (1 / smoothness)), int(img_height2 * (1 / smoothness))), interpolation=cv2.INTER_AREA)
sketch = cv2.cvtColor(sketch, cv2.COLOR_GRAY2BGR)
sketch = darken_image(sketch, details, line_intensity)
sketch = contrast_image(sketch, details, contrast)
if noise_removal > 0:
sketch = remove_noise_from_sketch(sketch, noise_removal)
sketch = cv2.cvtColor(sketch, cv2.COLOR_GRAY2BGR)
output_image_tensor = torch.from_numpy(sketch).permute(2, 0, 1).unsqueeze(0).float()/255
output_image_tensor = output_image_tensor.permute(0, 2, 3, 1)
gray_image = cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR)
gray_image = torch.from_numpy(gray_image).permute(2, 0, 1).unsqueeze(0).float()/255
gray_image = gray_image.permute(0, 2, 3, 1)
return output_image_tensor, gray_image
def processImg2Sketch(image, artist, line_strength, shading_effect, line_color, details, smoothness, enable_bg_color_change, bg_color, bg_light, noise_removal):
if image is None:
raise ValueError("Input image is required")
if isinstance(image, torch.Tensor):
image = image.squeeze(0).cpu().numpy()
image = convert_to_uint8(image)
if image.ndim not in [2, 3]:
raise ValueError("Input image must be 2D or 3D numpy array")
img_height, img_width = image.shape[:2]
# print(f"The width of the image is: {img_width}, and the height is: {img_height}")
line_intensity = 11 - line_strength
bg_light = bg_light/10
image = cv2.resize(image, (int(img_width * smoothness), int(img_height * smoothness)), interpolation=cv2.INTER_AREA)
img_height2, img_width2 = image.shape[:2]
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurEffect_s = shading_effect
blurred_image_s = cv2.GaussianBlur(gray_image, (blurEffect_s, blurEffect_s), 0)
sketch = cv2.divide(gray_image, blurred_image_s, scale=256.0)
sketch = cv2.resize(sketch, (int(img_width2 * (1 / smoothness)), int(img_height2 * (1 / smoothness))), interpolation=cv2.INTER_AREA)
if noise_removal > 0:
sketch = remove_noise_from_sketch(sketch, noise_removal)
sketch = cv2.cvtColor(sketch, cv2.COLOR_GRAY2BGR)
if artist == "1":
sketch = new_ink(sketch, details, line_intensity, getColor(line_color), True)
else:
sketch = new_ink2(sketch, details, line_intensity, getColor(line_color))
if enable_bg_color_change:
sketch = new_bg(sketch, details, bg_light, getColor(bg_color))
output_image_tensor = torch.from_numpy(sketch).permute(2, 0, 1).unsqueeze(0).float()/255
output_image_tensor = output_image_tensor.permute(0, 2, 3, 1)
gray_image = cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR)
gray_image = torch.from_numpy(gray_image).permute(2, 0, 1).unsqueeze(0).float()/255
gray_image = gray_image.permute(0, 2, 3, 1)
return output_image_tensor, gray_image
def processImg2LineArt(image, line_thickness, Clean_up, deep_Clean_up, line_color, color_strength, details, smoothness, bg_color, bg_light):
if image is None:
raise ValueError("Input image is required")
if isinstance(image, torch.Tensor):
image = image.squeeze(0).cpu().numpy()
image = convert_to_uint8(image)
if image.ndim not in [2, 3]:
raise ValueError("Input image must be 2D or 3D numpy array")
img_height, img_width = image.shape[:2]
# print(f"The width of the image is: {img_width}, and the height is: {img_height}")
image = cv2.resize(image, (int(img_width * smoothness), int(img_height * smoothness)), interpolation=cv2.INTER_AREA)
img_height2, img_width2 = image.shape[:2]
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
line_intensity = 11 - color_strength
detail = 10-details
bg_light = bg_light/10
if Clean_up > 1:
blurEffect_s = Clean_up
blurred_image_s = cv2.GaussianBlur(gray_image, (blurEffect_s, blurEffect_s), 0)
blurred_image_s = convert_to_uint8(blurred_image_s)
sketch = cv2.adaptiveThreshold(blurred_image_s, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, line_thickness, detail)
else:
gray_imageN = convert_to_uint8(gray_image)
sketch = cv2.adaptiveThreshold(gray_imageN, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, line_thickness, detail)
sketch = cv2.resize(sketch, (int(img_width2 * (1 / smoothness)), int(img_height2 * (1 / smoothness))), interpolation=cv2.INTER_AREA)
if deep_Clean_up > 0:
sketch = remove_noise_from_sketch(sketch, deep_Clean_up)
sketch = cv2.cvtColor(sketch, cv2.COLOR_GRAY2BGR)
sketch = new_ink2(sketch, details, line_intensity, getColor(line_color))
sketch = new_bg(sketch, details, bg_light, getColor(bg_color))
output_image_tensor = torch.from_numpy(sketch).permute(2, 0, 1).unsqueeze(0).float()/255
output_image_tensor = output_image_tensor.permute(0, 2, 3, 1)
gray_image = cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR)
gray_image = torch.from_numpy(gray_image).permute(2, 0, 1).unsqueeze(0).float()/255
gray_image = gray_image.permute(0, 2, 3, 1)
return output_image_tensor, gray_image
def processLineArt(image, details, deep_Clean_up, line_color, color_strength, bg_color, bg_light, invert = True, invert_default = True):
if image is None:
raise ValueError("Input image is required")
if isinstance(image, torch.Tensor):
image = image.squeeze(0).cpu().numpy()
image = convert_to_uint8(image)
if image.ndim not in [2, 3]:
raise ValueError("Input image must be 2D or 3D numpy array")
originalLineArt = image
sketch = np.copy(originalLineArt)
if invert:
sketch = 255 - sketch
if invert_default:
originalLineArt = 255 - originalLineArt
line_intensity = 11 - color_strength
bg_light = bg_light/10
if deep_Clean_up > 0:
sketch = remove_noise_from_sketch(sketch, deep_Clean_up)
sketch = cv2.cvtColor(sketch, cv2.COLOR_GRAY2BGR)
sketch = new_ink2(sketch, details, line_intensity, getColor(line_color))
sketch = new_bg(sketch, details, bg_light, getColor(bg_color))
output_image_tensor = torch.from_numpy(sketch).permute(2, 0, 1).unsqueeze(0).float()/255
output_image_tensor = output_image_tensor.permute(0, 2, 3, 1)
originalLineArt = torch.from_numpy(originalLineArt).permute(2, 0, 1).unsqueeze(0).float()/255
originalLineArt = originalLineArt.permute(0, 2, 3, 1)
return output_image_tensor, originalLineArt
NODE_CLASS_MAPPINGS = {
"Sketch_Assistant_grayScale" : Sketch_Assistant_grayScale,
"Sketch_Assistant" : Sketch_Assistant,
"LineArt_Assistant": LineArt_Assistant,
"LineArt_Assistant_2": LineArt_Assistant_2,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"Sketch_Assistant_grayScale" :"Img2Sketch Assistant (Grayscale)",
"Sketch_Assistant" :"Img2Sketch Assistant ",
"LineArt_Assistant": "Img2LineArt Assistant",
"LineArt_Assistant_2": "lineArt2LineArt Assistant",
}