-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUtil.py
1018 lines (860 loc) · 32.7 KB
/
Util.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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import math
import random
import torch
import numpy as np
from PIL import Image
import cv2
from .color_transfer import ColorTransfer
from comfy_extras.nodes_upscale_model import ImageUpscaleWithModel
import torchvision.transforms as T
import torchvision.transforms.functional as con
class AlwaysEqualProxy(str):
#ComfyUI-Logic
#refer: https://github.com/theUpsider/ComfyUI-Logic
def __eq__(self, _):
return True
def __ne__(self, _):
return False
cat = "Mira/Util"
cat_image = "Mira/Util/Image"
def DecodeImage(src_image):
i = 255. * src_image[0].cpu().numpy()
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
return img
def EncodeImage(src_image):
img = np.array(src_image).astype(np.float32) / 255.0
img = torch.from_numpy(img)[None,]
return img
def ConvertToNP(src_image):
i = 255. * src_image[0].cpu().numpy()
array_image = np.clip(i, 0, 255).astype(np.uint8)
return array_image.astype(np.float32)
def SafeCheck(Width = 16, Height = 16, Batch = 1, HiResMultiplier = 1.0):
if 16 > Width:
Width = 16
if 16 > Height:
Height = 16
if 1 > Batch:
Batch = 1
if 1.0 > HiResMultiplier:
HiResMultiplier = 1.0
return Width, Height, Batch, HiResMultiplier
def Fixeight(num):
new_num = int(num)
if 0 != math.floor(num)%8:
residue = math.floor(num)%8
if 3 >= math.floor(num)%8:
new_num = math.floor(num) - residue
else:
new_num = math.floor(num) + 8 - residue
return new_num
class CanvasCreatorBasic:
'''
Create Canvas information Width and Height for Latent.
Inputs:
Width - Image Width
Height - Image Height
Outputs:
Width - Image Width
Height - Image Height
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"Width": ("INT", {
"default": 576,
"min": 16,
"max": 4096,
"step": 8,
"display": "number"
}),
"Height": ("INT", {
"default": 1024,
"min": 16,
"max": 4096,
"step": 8,
"display": "number"
}),
},
}
RETURN_TYPES = ("INT","INT",)
RETURN_NAMES = ("Width","Height",)
FUNCTION = "CanvasCreatorBasicEx"
CATEGORY = cat
def CanvasCreatorBasicEx(self, Width, Height):
Width, Height, Batch, HiResMultiplier = SafeCheck(Width, Height)
return(Width, Height,)
class CanvasCreatorSimple:
'''
Create Canvas information Width and Height for Latent with Landscape switch.
Inputs:
Width - Image Width
Height - Image Height
Landscape - When ENABLED, will swap Width and Height for output
Outputs:
Width - Image Width
Height - Image Height
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"Width": ("INT", {
"default": 576,
"min": 16,
"max": 4096,
"step": 8,
"display": "number"
}),
"Height": ("INT", {
"default": 1024,
"min": 16,
"max": 4096,
"step": 8,
"display": "number"
}),
"Landscape": ("BOOLEAN", {
"default": False
}),
},
}
RETURN_TYPES = ("INT","INT",)
RETURN_NAMES = ("Width","Height",)
FUNCTION = "CanvasCreatorSimpleEx"
CATEGORY = cat
def CanvasCreatorSimpleEx(self, Width, Height, Landscape):
Width, Height, Batch, HiResMultiplier = SafeCheck(Width, Height)
if(False == Landscape):
return(Width, Height,)
else:
return(Height, Width,)
class CanvasCreatorAdvanced:
'''
Create Canvas information Width and Height for Latent with Landscape switch, Batch and HiResMultiplier.
Inputs:
Width - Image Width
Height - Image Height
Landscape - When ENABLED, will swap Width and Height for output
Batch - Batch size for Latent
HiResMultiplier - Multiplier setting for high-resolution output
Outputs:
Width - Image Width for Latent
Height - Image Height for Latent
Batch - Batch size for Latent
HiRes Width - Width x HiResMultiplier. The result is not the product of the original data, but the nearest multiple of 8.
HiRes Height - Height x HiResMultiplier.
HiResMultiplier - Same as Input
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"Width": ("INT", {
"default": 576,
"min": 16,
"max": 4096,
"step": 8,
"display": "number"
}),
"Height": ("INT", {
"default": 1024,
"min": 16,
"max": 4096,
"step": 8,
"display": "number"
}),
"Batch": ("INT", {
"default": 1,
"min": 1,
"max": 16,
"step": 1,
"display": "number"
}),
"Landscape": ("BOOLEAN", {
"default": False
}),
"HiResMultiplier": ("FLOAT", {
"default": 1.5,
"min": 0.1,
"max": 8,
"step": 0.1,
"display": "number"
}),
},
}
RETURN_TYPES = ("INT","INT","INT","INT","INT","FLOAT")
RETURN_NAMES = ("Width","Height","Batch","HiRes Width","HiRes Height","HiResMultiplier",)
FUNCTION = "CanvasCreatorEx"
CATEGORY = cat
def CanvasCreatorEx(self, Width, Height, Batch, Landscape, HiResMultiplier):
Width, Height, Batch, HiResMultiplier = SafeCheck(Width, Height, Batch, HiResMultiplier)
HiResWidth = Fixeight(Width * HiResMultiplier)
HiResHeight = Fixeight(Height * HiResMultiplier)
if(False == Landscape):
intHiResHeight = math.floor(HiResHeight)
intHiResWidth = math.floor(HiResWidth)
return(Width, Height, Batch, intHiResWidth, intHiResHeight, HiResMultiplier, )
else:
intHiResHeight = math.floor(HiResHeight)
intHiResWidth = math.floor(HiResWidth)
return(Height, Width, Batch, intHiResHeight, intHiResWidth, HiResMultiplier, )
class RandomTillingLayouts:
'''
[#1](https://github.com/mirabarukaso/ComfyUI_Mira/issues/1)
Random Tilling Mask Layout Generator
Highly recommend connect the output `layout` or `Create Tilling PNG Mask -> Debug` to `ShowText` node.
**Known Issue** about `Seed Generator`
Switching `randomize` to `fixed` now works immediately.
But, switching `fixed` to `randomize`, it need 2 times `Queue Prompt` to take affect. (Because of the ComfyUI logic)
Solution: Try `Global Seed (Inspire)` from [ComfyUI-Inspire-Pack](https://github.com/ltdrdata/ComfyUI-Inspire-Pack)
**Reminder **
The `rnd_seed` have nothing to do with the actual random numbers,
you can't get the same `layout` with the same `rnd_seed`,
it is recommended to use `ShowText` and `Notes` to save your favourite `layout`.
**Hint**
Set rows or colums to `0` for only one direction cuts.
Whichever is set to `0` will automatically cut according to the other non-zero setting.
Just in case all fours are `0`, it will return `1,1`.
Inputs:
min_rows, max_rows - Range of how many `N cuts` you may want, set both to 0 to disable it.
min_colums, max_colums - Range of how many `G cuts` you may want, set both to 0 to disable it.
max_weights_gcuts - The maxium weight of `G cuts` range from 1 to `max_weights_gcuts`
max_weights_ncuts - The maxium weight of `N cuts` range from 1 to `max_weights_ncuts`
rnd_seed - Connect to the `Seed Generator` node, then use `Global Seed (Inspire)` node to control it properly.
Outputs:
Layout - Layouts string, you need connect it to `Create Tilling PNG Mask -> layout`
Example:
[2,2,2,1]@3.8,4.2,2.1,3.3;3.6,3.5,3.3,3.7;2.7,3.2,4.9
^ * * *
^2 colums
* the 1st block has 2 rows (3 blocks)
* the 2nd block has 2 rows (3 blocks)
* the 3rd block has 1 row (2 blocks)
3+3+2= 8 blocks in total
Colum_first == False
1|4|7
2|5|
3|6|8
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"min_rows": ("INT", {
"default": 1,
"min": 0,
"max": 16,
"step": 1,
"display": "number"
}),
"max_rows": ("INT", {
"default": 1,
"min": 0,
"max": 16,
"step": 1,
"display": "number"
}),
"min_colums": ("INT", {
"default": 1,
"min": 0,
"max": 16,
"step": 1,
"display": "number"
}),
"max_colums": ("INT", {
"default": 1,
"min": 0,
"max": 16,
"step": 1,
"display": "number"
}),
"max_weights_gcuts": ("FLOAT", {
"default": 2.0,
"min": 1.0,
"max": 10.0,
"step": 0.1,
"display": "number"
}),
"max_weights_ncuts": ("FLOAT", {
"default": 2.0,
"min": 1.0,
"max": 10.0,
"step": 0.1,
"display": "number"
}),
"rnd_seed": (AlwaysEqualProxy('*'), {
"default": 0,
"min": 0,
"max": 0xffffffffffffffff,
"display": "input"
}),
},
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("layout",)
FUNCTION = "RandomTillingLayoutsEx"
CATEGORY = cat
def RandomTillingLayoutsEx(self, min_rows, max_rows, min_colums, max_colums, max_weights_gcuts, max_weights_ncuts, rnd_seed):
if min_colums > max_colums:
min_colums = max_colums
if min_rows > max_rows:
min_rows = max_rows
if 0 == max_colums and max_colums == max_rows:
layouts = "1,1"
return (layouts,)
if 0 == max_colums or 0 == max_rows:
max_colums = max(max_colums, max_rows)
colums = random.randrange(min_colums, max_colums + 1)
#print("Mira: colums: " + str(colums))
random.seed(rnd_seed)
row_and_colum_info = '[' + str(rnd_seed) + '][' + str(colums) + ","
layouts = ""
if 0 == colums:
rows = random.randrange(min_rows, max_rows + 1)
#print("Mira: colums = 0 rows: " + str(rows))
if 0 == rows:
row_and_colum_info += "0,"
layouts = "1,1"
else:
row_and_colum_info += str(rows) + ","
for _ in range(0, rows + 1):
layouts += str(round(random.uniform(1, max_weights_ncuts),1))
layouts += ","
layouts = layouts[:-1]
else:
for _ in range(0, colums + 1):
layouts += str(round(random.uniform(1, max_weights_gcuts),1))
rows = random.randrange(min_rows, max_rows + 1)
#print("Mira: rows: " + str(rows))
if 0 == rows:
row_and_colum_info += "0,"
layouts += ",1"
else:
row_and_colum_info += str(rows) + ","
layouts += ","
for _ in range(0, rows + 1):
layouts += str(round(random.uniform(1, max_weights_ncuts),1))
layouts += ","
layouts = layouts[:-1]
layouts += ";"
layouts = layouts[:-1]
row_and_colum_info = row_and_colum_info[:-1]
row_and_colum_info += ']@'
return (row_and_colum_info + layouts,)
class RandomNestedLayouts:
'''
Random Nested Mask Layout Generator
All known issues same as upper one.
Inputs:
min_nested, max_nested - Range of nest you want.
min_weights, max_weights - The weight of every nest.
rnd_seed - Connect to the `Seed Generator` node, then use `Global Seed (Inspire)` node to control it properly.
Outputs:
Layout - Layouts string, you need connect it to `Create Nested PNG Mask -> layout`
top, bottom, left, right - Random Boolean
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"min_nested": ("INT", {
"default": 1,
"min": 1,
"max": 32,
"step": 1,
"display": "number"
}),
"max_nested": ("INT", {
"default": 2,
"min": 1,
"max": 32,
"step": 1,
"display": "number"
}),
"min_weights": ("FLOAT", {
"default": 1.0,
"min": 0.1,
"max": 10.0,
"step": 0.1,
"display": "number"
}),
"max_weights": ("FLOAT", {
"default": 2.0,
"min": 1.0,
"max": 10.0,
"step": 0.1,
"display": "number"
}),
"rnd_seed": (AlwaysEqualProxy('*'), {
"default": 0,
"min": 0,
"max": 0xffffffffffffffff,
"display": "input"
}),
},
}
RETURN_TYPES = ("STRING", "BOOLEAN", "BOOLEAN", "BOOLEAN", "BOOLEAN")
RETURN_NAMES = ("layout", "top", "bottom", "left", "right")
FUNCTION = "RandomNestedLayoutsEx"
CATEGORY = cat
def RandomNestedLayoutsEx(self, min_nested, max_nested, min_weights, max_weights, rnd_seed):
if min_nested > max_nested:
min_nested = max_nested
if min_weights > max_weights:
min_weights = max_weights
nested = random.randrange(min_nested, max_nested + 1)
bool1 = bool(random.getrandbits(1))
bool2 = bool(random.getrandbits(1))
bool3 = bool(random.getrandbits(1))
bool4 = bool(random.getrandbits(1))
random.seed(rnd_seed)
generator_info = '[' + str(rnd_seed) + '][' + str(nested) + '][' + str(bool1) + ',' + str(bool2) + ',' + str(bool3) + ',' + str(bool4) + ']@'
layouts = ""
for _ in range(0, nested):
layouts += str(round(random.uniform(min_weights, max_weights),1))
layouts += ','
layouts = layouts[:-1]
return (generator_info + layouts, bool1, bool2, bool3, bool4,)
class SeedGenerator:
'''
SeedGenerator
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"seed": ("INT", {
"default": 0,
"min": 0,
"max": 0xffffffffffffffff
}),
},
}
RETURN_TYPES = ("INT",)
RETURN_NAMES = ("seed",)
FUNCTION = "SeedGeneratorEx"
CATEGORY = cat
def SeedGeneratorEx(self, seed):
return(seed,)
class ImageGrayscale:
'''
Convert Image to Grayscale
Inputs:
src_image - Source Image
Outputs:
image - Torched Image
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"src_image": ("IMAGE", {
"default": None,
}),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "ImageGrayscaleEx"
CATEGORY = cat_image
def ImageGrayscaleEx(self, src_image):
img = DecodeImage(src_image)
img_adj = con.to_grayscale(img)
result = EncodeImage(img_adj)
return(result,)
class ImageContrast:
'''
Adjust Image Contrast
Inputs:
src_image - Source Image
level - Contrast Level, default is 1.0
Outputs:
image - Torched Image
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"src_image": ("IMAGE", {
"default": None,
}),
"level": ("FLOAT", {
"default": 1.0,
"step": 0.001,
"min": 0,
"max": 10
}),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "ImageContrastEx"
CATEGORY = cat_image
def ImageContrastEx(self, src_image, level):
img = DecodeImage(src_image)
img_adj = con.adjust_contrast(img, level)
result = EncodeImage(img_adj)
return(result,)
class ImageSharpness:
'''
Adjust Image Sharpness
Inputs:
src_image - Source Image
level - Sharpness Level, default is 1.0
Outputs:
image - Torched Image
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"src_image": ("IMAGE", {
"default": None,
}),
"level": ("FLOAT", {
"default": 1.0,
"step": 0.1,
"min": 0,
"max": 100
}),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "ImageSharpnessEx"
CATEGORY = cat_image
def ImageSharpnessEx(self, src_image, level):
img = DecodeImage(src_image)
img_adj = con.adjust_sharpness(img, level)
result = EncodeImage(img_adj)
return(result,)
class ImageBrightness:
'''
Adjust Image Brightness
Inputs:
src_image - Source Image
level - Brightness Level, default is 1.0
Outputs:
image - Torched Image
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"src_image": ("IMAGE", {
"default": None,
}),
"level": ("FLOAT", {
"default": 1.0,
"step": 0.001,
"min": 0,
"max": 10
}),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "ImageBrightnessEx"
CATEGORY = cat_image
def ImageBrightnessEx(self, src_image, level):
img = DecodeImage(src_image)
img_adj = con.adjust_brightness(img, level)
result = EncodeImage(img_adj)
return(result,)
class ImageSaturation:
'''
Adjust Image Saturation
Inputs:
src_image - Source Image
level - Saturation Level, default is 0.0
Outputs:
image - Torched Image
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"src_image": ("IMAGE", {
"default": None,
}),
"level": ("FLOAT", {
"default": 0.0,
"step": 0.001,
"min": 0,
"max": 10
}),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "ImageSaturationEx"
CATEGORY = cat_image
def ImageSaturationEx(self, src_image, level):
img = DecodeImage(src_image)
img_adj = con.adjust_saturation(img, level)
result = EncodeImage(img_adj)
return(result,)
class ImageHUE:
'''
Adjust Image HUE
Inputs:
src_image - Source Image
level - HUE Level, default is 0.0
Outputs:
image - Torched Image
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"src_image": ("IMAGE", {
"default": None,
}),
"level": ("FLOAT", {
"default": 0.0,
"step": 0.001,
"min": -0.5,
"max": 0.5
}),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "ImageHUEEx"
CATEGORY = cat_image
def ImageHUEEx(self, src_image, level):
img = DecodeImage(src_image)
img_adj = con.adjust_hue(img, level)
result = EncodeImage(img_adj)
return(result,)
class ImageGamma:
'''
Adjust Image Gamma
Inputs:
src_image - Source Image
level - Gamma Level, default is 0.0
Outputs:
image - Torched Image
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"src_image": ("IMAGE", {
"default": None,
}),
"level": ("FLOAT", {
"default": 0.0,
"step": 0.001,
"min": 0,
"max": 10
}),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "ImageGammaEx"
CATEGORY = cat_image
def ImageGammaEx(self, src_image, level):
img = DecodeImage(src_image)
img_adj = con.adjust_gamma(img, level)
result = EncodeImage(img_adj)
return(result,)
class ImageColorTransfer:
'''
Refer to: https://en.wikipedia.org/wiki/Image_color_transfer
Image Color Transfer
Inputs:
src_image - Source Image
ref_image - Reference image. The colors of this Image will applied to the Source Image
Outputs:
image - Output Image
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"src_image": ("IMAGE", {
"default": None,
}),
"ref_image": ("IMAGE", {
"default": None,
}),
"method" : (['Mean', 'Lab', 'Pdf', 'Pdf+Regrain'], ),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "ImageColorTransferEx"
CATEGORY = cat_image
def ImageColorTransferEx(self, src_image, ref_image, method):
PT = ColorTransfer()
new_img = None
if "Mean" == method:
s = ConvertToNP(src_image)
r = ConvertToNP(ref_image)
new_img = PT.mean_std_transfer(img_arr_in=s, img_arr_ref=r)
elif "Lab" == method:
s = np.array(DecodeImage(src_image), dtype=np.uint8)
r = np.array(DecodeImage(ref_image), dtype=np.uint8)
new_img = PT.lab_transfer(img_arr_in=s, img_arr_ref=r)
elif "Pdf" == method:
s = ConvertToNP(src_image)
r = ConvertToNP(ref_image)
new_img = PT.pdf_transfer(img_arr_in=s, img_arr_ref=r, regrain=False)
else:
s = ConvertToNP(src_image)
r = ConvertToNP(ref_image)
new_img = PT.pdf_transfer(img_arr_in=s, img_arr_ref=r, regrain=True)
result = EncodeImage(new_img)
return(result,)
class ImageToneCurve:
'''
Image Tone Curve
Adjust the overall brightness using the `RGB Channels` or `Brightness` node.
Inputs:
src_image - Source Image
low - Increase shadow range
high - Increase highlight range
Outputs:
image - Output Image
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"src_image": ("IMAGE", {
"default": None,
}),
"low": ("FLOAT", {
"default": -1.0,
"step": 0.01,
"min": -10,
"max": 10
}),
"high": ("FLOAT", {
"default": 1.0,
"step": 0.01,
"min": -10,
"max": 10
}),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "ImageToneCurveEx"
CATEGORY = cat_image
def ImageToneCurveEx(self, src_image, low, high):
y = np.arctan(np.linspace(low, high, 256))
y = 255 / (y.max() - y.min()) * (y - y.max()) + 255
s = np.array(DecodeImage(src_image), dtype=np.uint8)
new_img = cv2.LUT(s, y).astype(np.uint8)
result = EncodeImage(new_img)
return(result,)
class ImageRGBChannel:
'''
Image RGB Channel
Inputs:
src_image - Source Image
R/G/B - Colour magnification value, less than 1.0 means attenuation
Outputs:
image - Output Image
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"src_image": ("IMAGE", {
"default": None,
}),
"Red": ("FLOAT", {
"default": 1.0,
"step": 0.01,
"min": 0,
"max": 5
}),
"Green": ("FLOAT", {
"default": 1.0,
"step": 0.01,
"min": 0,
"max": 5
}),
"Blue": ("FLOAT", {
"default": 1.0,
"step": 0.01,
"min": 0,
"max": 5
}),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "ImageRGBChannelEx"
CATEGORY = cat_image
def ImageRGBChannelEx(self, src_image, Red, Green, Blue):
s = DecodeImage(src_image)
r, g, b = s.split()
r = r.point(lambda i: i * Red)
g = g.point(lambda i: i * Green)
b = b.point(lambda i: i * Blue)
new_img = Image.merge('RGB', (r, g, b))
result = EncodeImage(new_img)
return(result,)
class UpscaleImageByModelThenResize:
'''
Upscale Image By Model Then Resize
This is an experimental feature for zooming in an image on a model and then zooming out to a specified size (a multiple of 8 in length and width).
For example, if the input model zooms the image 4x by default and the node is set to zoom 2x, then the image will first be zoomed 4x using the model and then resized to 2x.
Inputs:
upscale_model - Model for upscaling
image - Source Image
resize_scale - Real resize ratio, the result will be the nearest multiple of 8.
resize_method - Resize method, nearest, nearest-exact, bilinear, bicubic
Outputs:
image - Output Image
'''
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"upscale_model": ("UPSCALE_MODEL",),
"image": ("IMAGE", {
"default": None,
}),
"resize_scale": ("FLOAT", {
"default": 1.5,
"step": 0.1,
"min": 0.1,
"max": 8
}),
"resize_method" : (['nearest', 'nearest-exact', 'bilinear', 'bicubic'], ),
},
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "UpscaleImageWithModelEx"
CATEGORY = cat_image
def UpscaleImageWithModelEx(self, upscale_model, image, resize_scale, resize_method):
new_img = (ImageUpscaleWithModel.upscale(self, upscale_model, image))[0]
#print('resize_scale: ' + str(resize_scale))
#print('upscale_model.scale: ' + str(upscale_model.scale))
if upscale_model.scale != resize_scale:
width = image.shape[2]
height = image.shape[1]
new_width = Fixeight(width*resize_scale)
new_height = Fixeight(height*resize_scale)
#print('new_width: ' + str(new_width))