-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.py
350 lines (296 loc) · 10.6 KB
/
image.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
from .modules import logger as loggerUtil, imageUtils, miscUtils, samlib
from PIL import Image, ImageOps, ImageSequence, ImageDraw
import cv2
import numpy as np
import torch
import folder_paths
import hashlib
import os
import node_helpers
import math
class ImageFillColorByMask:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
"mask": ("MASK",),
"color": ("STRING", {"default": "#ffffff"}),
"mode": (["RGB", "RGBA"], {"default": "RGB"}),
}
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "generate"
CATEGORY = "TinyUtils"
def generate(self, image, mask, color="#ffffff", mode="RGB"):
image = imageUtils.tensor2pil(image)
mask = imageUtils.tensor2pil(mask)
output_image = imageUtils.fillColorByMask(image, mask, color, mode)
output_image = imageUtils.pil2comfy(output_image, mode)
return (torch.cat([output_image], dim=0),)
class CropImageByMask:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
"mask": ("MASK",),
"color": ("STRING", {"default": "#ffffff"}),
"mode": (["RGB", "RGBA"], {"default": "RGB"}),
}
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "generate"
CATEGORY = "TinyUtils"
def generate(self, image, mask, color="#ffffff", mode="RGB"):
image = imageUtils.tensor2pil(image)
mask = imageUtils.tensor2pil(mask)
output_image = imageUtils.cropImageByMask(image, mask, color, mode)
output_image = imageUtils.pil2comfy(output_image, mode)
return (torch.cat([output_image], dim=0),)
class LoadImageAdvance:
@classmethod
def INPUT_TYPES(s):
input_dir = folder_paths.get_input_directory()
files = [
f
for f in os.listdir(input_dir)
if os.path.isfile(os.path.join(input_dir, f))
]
return {
"required": {
"image": (sorted(files), {"image_upload": True}),
"mode": (["RGB", "RGBA"], {"default": "RGB"}),
},
}
CATEGORY = "TinyUtils"
RETURN_TYPES = ("IMAGE", "MASK")
FUNCTION = "load_image_adv"
def load_image_adv(self, image, mode="RGB"):
image_path = folder_paths.get_annotated_filepath(image)
img = node_helpers.pillow(Image.open, image_path)
output_images = []
output_masks = []
w, h = None, None
excluded_formats = ["MPO"]
for i in ImageSequence.Iterator(img):
i = node_helpers.pillow(ImageOps.exif_transpose, i)
if i.mode == "I":
i = i.point(lambda i: i * (1 / 255))
if mode == "RGBA":
image = i.convert("RGBA")
else:
image = i.convert("RGB")
if len(output_images) == 0:
w = image.size[0]
h = image.size[1]
if image.size[0] != w or image.size[1] != h:
continue
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
if "A" in i.getbands():
mask = np.array(i.getchannel("A")).astype(np.float32) / 255.0
mask = 1.0 - torch.from_numpy(mask)
else:
mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu")
output_images.append(image)
output_masks.append(mask.unsqueeze(0))
if len(output_images) > 1 and img.format not in excluded_formats:
output_image = torch.cat(output_images, dim=0)
output_mask = torch.cat(output_masks, dim=0)
else:
output_image = output_images[0]
output_mask = output_masks[0]
return (output_image, output_mask)
@classmethod
def IS_CHANGED(s, image, mode):
image_path = folder_paths.get_annotated_filepath(image)
m = hashlib.sha256()
with open(image_path, "rb") as f:
m.update(f.read())
return m.digest().hex()
@classmethod
def VALIDATE_INPUTS(s, image, mode):
if not folder_paths.exists_annotated_filepath(image):
return "Invalid image file: {}".format(image)
return True
class ImageTransposeAdvance:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
"image_overlay": ("IMAGE",),
"width": (
"INT",
{"default": 512, "min": -48000, "max": 48000, "step": 1},
),
"height": (
"INT",
{"default": 512, "min": -48000, "max": 48000, "step": 1},
),
"X": ("INT", {"default": 0, "min": -48000, "max": 48000, "step": 1}),
"Y": ("INT", {"default": 0, "min": -48000, "max": 48000, "step": 1}),
"rotation": (
"FLOAT",
{"default": 0, "min": -360, "max": 360, "step": 0.01},
),
"feathering": ("INT", {"default": 0, "min": 0, "max": 4096, "step": 1}),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "image_transpose"
CATEGORY = "TinyUtils"
def image_transpose(
self,
image: torch.Tensor,
image_overlay: torch.Tensor,
width: int,
height: int,
X: int,
Y: int,
rotation: int,
feathering: int = 0,
):
return (
imageUtils.pil2tensor(
self.apply_transpose_image(
imageUtils.tensor2pil(image),
imageUtils.tensor2pil(image_overlay),
(width, height),
(X, Y),
rotation,
feathering,
)
),
)
def calculate_differ(
self,
pos: tuple[int, int],
oriSize: tuple[int, int],
newSize: tuple[int, int],
rotate: int,
) -> tuple[int, int]:
w = oriSize[0]
h = oriSize[1]
p = miscUtils.rotatePoint(
pos, (math.floor(pos[0] + w / 2), math.floor(pos[1] + h / 2)), rotate
)
x = 0
y = math.floor(newSize[1] / 2) + pos[1] - p[1]
if rotate > 0:
x = math.floor(newSize[0] / 2) + pos[0] - p[0]
y = 0
if rotate > 90 or rotate < -90:
x = math.floor(newSize[0] / 2) + pos[0] - p[0]
y = math.floor(newSize[1] / 2) + pos[1] - p[1]
return (x, y)
def apply_transpose_image(
self, image_bg: Image, image_element: Image, size, loc, rotate=0, feathering=0
):
# Apply transformations to the element image
image_element = image_element.resize(size)
image_element = image_element.rotate(
-rotate, expand=True, resample=Image.Resampling.BICUBIC
)
imgSize = image_element.size
# Create a mask for the image with the faded border
if feathering > 0:
mask = Image.new("L", imgSize, 255) # Initialize with 255 instead of 0
draw = ImageDraw.Draw(mask)
for i in range(feathering):
alpha_value = int(
255 * (i + 1) / feathering
) # Invert the calculation for alpha value
draw.rectangle(
(i, i, imgSize[0] - i, imgSize[1] - i),
fill=alpha_value,
)
alpha_mask = Image.merge("RGBA", (mask, mask, mask, mask))
image_element = Image.composite(
image_element,
Image.new("RGBA", imgSize, (0, 0, 0, 0)),
alpha_mask,
)
differ = self.calculate_differ(loc, size, imgSize, rotate)
loc = (loc[0] - differ[0], loc[1] - differ[1])
# Create a new image of the same size as the base image with an alpha channel
new_image = Image.new("RGBA", image_bg.size, (0, 0, 0, 0))
new_image.paste(image_element, loc)
# Paste the new image onto the base image
image_bg = image_bg.convert("RGBA")
image_bg.paste(new_image, (0, 0), new_image)
return image_bg
class ImageSAMMask:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
"model": (
[
"sam_hq_vit_b.pth",
"sam_hq_vit_h.pth",
"sam_hq_vit_l.pth",
"sam_vit_b_01ec64.pth",
"sam_vit_h_4b8939.pth",
"sam_vit_l_0b3195.pth",
],
{"default": "sam_hq_vit_h.pth"},
),
"points_per_side": (
"INT",
{"default": 32, "min": 0, "max": 1000000, "step": 1},
),
"pred_iou_thresh": (
"FLOAT",
{"default": 0.86, "min": 0, "max": 1, "step": 0.01},
),
"stability_score_thresh": (
"FLOAT",
{"default": 0.92, "min": 0, "max": 1, "step": 0.01},
),
"crop_n_layers": (
"INT",
{"default": 0, "min": 0, "max": 100, "step": 1},
),
"crop_n_points_downscale_factor": (
"INT",
{"default": 1, "min": 0, "max": 100, "step": 1},
),
"min_mask_region_area": (
"INT",
{"default": 0, "min": 0, "max": 100, "step": 1},
),
}
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "generate"
CATEGORY = "TinyUtils"
def generate(
self,
image,
model,
points_per_side,
pred_iou_thresh,
stability_score_thresh,
crop_n_layers,
crop_n_points_downscale_factor,
min_mask_region_area,
):
image = imageUtils.tensor2pil(image)
output_image = samlib.run_sam(
image,
model,
points_per_side,
pred_iou_thresh,
stability_score_thresh,
crop_n_layers,
crop_n_points_downscale_factor,
min_mask_region_area,
)
output_image = imageUtils.pil2comfy(output_image)
return (torch.cat([output_image], dim=0),)