forked from filliptm/ComfyUI_Fill-Nodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fl_glitch.py
63 lines (50 loc) · 1.91 KB
/
fl_glitch.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
import torch
import numpy as np
from PIL import Image
from glitch_this import ImageGlitcher
import sys
class FL_Glitch:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"images": ("IMAGE",),
},
"optional": {
"glitch_amount": ("FLOAT", {"default": 3.0, "min": 0.1, "max": 10.0, "step": 0.01}),
"color_offset": (["Disable", "Enable"],),
"seed": ("INT", {"default": 0, "min": 0, "max": 100, "step": 1}),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "glitch"
CATEGORY = "🏵️Fill Nodes"
def t2p(self, t):
if t is not None:
i = 255.0 * t.cpu().numpy().squeeze()
p = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
return p
def s2b(self, v):
return v == "Enable"
def glitch(self, images, glitch_amount=1, color_offset="Disable", seed=0):
color_offset = self.s2b(color_offset)
g = ImageGlitcher()
out = []
total_images = len(images)
for i, image in enumerate(images, start=1):
p = self.t2p(image)
g1 = g.glitch_image(p, glitch_amount, color_offset=color_offset, seed=seed)
r1 = g1.rotate(90, expand=True)
g2 = g.glitch_image(r1, glitch_amount, color_offset=color_offset, seed=seed)
f = g2.rotate(-90, expand=True)
o = np.array(f.convert("RGB")).astype(np.float32) / 255.0
o = torch.from_numpy(o).unsqueeze(0)
out.append(o)
# Print progress update
progress = i / total_images * 100
sys.stdout.write(f"\rProcessing images: {progress:.2f}%")
sys.stdout.flush()
# Print a new line after the progress update
print()
out = torch.cat(out, 0)
return (out,)