-
Notifications
You must be signed in to change notification settings - Fork 20
/
__init__.py
145 lines (107 loc) · 3.55 KB
/
__init__.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
"""
@author: Lerc
@title: Canvas Tab
@nickname: Canvas Tab
@description: This extension provides a full page image editor with mask support. There are two nodes, one to receive images from the editor and one to send images to the editor.
"""
import torch
import base64
import os
import folder_paths
from io import BytesIO
from PIL import Image, ImageOps
from PIL.PngImagePlugin import PngInfo
import numpy as np
def image_to_data_url(image):
buffered = BytesIO()
image.save(buffered, format="PNG")
img_base64 = base64.b64encode(buffered.getvalue())
return f"data:image/png;base64,{img_base64.decode()}"
class Send_To_Editor:
def __init__(self):
self.updateTick = 1
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
},
"hidden": {
"unique_id":"UNIQUE_ID",
},
"optional": {
"images": ("IMAGE",),
},
}
RETURN_TYPES = ()
FUNCTION = "collect_images"
OUTPUT_NODE = True
CATEGORY = "image"
def IS_CHANGED(self, unique_id, images):
self.updateTick+=1
return hex(self.updateTick)
def collect_images(self, unique_id, images=None):
collected_images = list()
if images is not None:
for image in images:
i = 255. * image.cpu().numpy()
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
collected_images.append(image_to_data_url(img))
return { "ui": {"collected_images":collected_images}}
class Canvas_Tab:
"""
A Image Buffer for handling an editor in another tab.
"""
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mask": ("CANVAS",),
"canvas": ("CANVAS",),
},
"hidden": {
"unique_id":"UNIQUE_ID",
},
# "optional": {
# "images": ("IMAGE",),
# },
}
RETURN_TYPES = ("IMAGE","MASK")
FUNCTION = "image_buffer"
#OUTPUT_NODE = False
CATEGORY = "image"
def image_buffer(self, unique_id, mask, canvas, images=None):
# collected_images = list()
# if images is not None:
# for image in images:
# i = 255. * image.cpu().numpy()
# img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
# collected_images.append(image_to_data_url(img))
#
# print(f"Node {unique_id}: images: {images}")
image_path = folder_paths.get_annotated_filepath(canvas)
i = Image.open(image_path)
i = ImageOps.exif_transpose(i)
rgb_image = i.convert("RGB")
rgb_image = np.array(rgb_image).astype(np.float32) / 255.0
rgb_image = torch.from_numpy(rgb_image)[None,]
mask_path = folder_paths.get_annotated_filepath(mask)
i = Image.open(mask_path)
i = ImageOps.exif_transpose(i)
if 'A' in i.getbands():
mask_data = np.array(i.getchannel('A')).astype(np.float32) / 255.0
mask_data = torch.from_numpy(mask_data)
else:
mask_data = torch.zeros((64,64), dtype=torch.float32, device="cpu")
return (rgb_image, mask_data)
WEB_DIRECTORY = "web"
NODE_CLASS_MAPPINGS = {
"Canvas_Tab": Canvas_Tab,
"Send_To_Editor": Send_To_Editor
}
NODE_DISPLAY_NAME_MAPPINGS = {
"Canvas_Tab": "Edit In Another Tab",
"Send_To_Editor": "Send to Editor Tab"
}