-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBlackBarsV1.py
122 lines (101 loc) · 3.98 KB
/
BlackBarsV1.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
import torch
import torch.nn.functional as F
from typing import Tuple
class BlackBarsV1:
"""
ComfyUI custom node that applies letterboxing or pillarboxing to a sequence of images.
Adds black bars either horizontally (letterbox) or vertically (pillarbox).
"""
def __init__(self):
self.device = "cuda" if torch.cuda.is_available() else "cpu"
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"images": ("IMAGE",),
"box_mode": (["letterbox", "pillarbox"],),
"bar_size": ("INT", {
"default": 100,
"min": 0,
"max": 500,
"step": 1,
"display": "slider"
}),
"bar_feather": ("INT", {
"default": 0,
"min": 0,
"max": 50,
"step": 1,
"display": "slider",
"description": "Softens the edge of the bars"
})
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "apply_black_bars"
CATEGORY = "image/format"
def create_mask(self, height: int, width: int, box_mode: str,
bar_size: int, feather: int) -> torch.Tensor:
"""Creates a mask for the letterbox/pillarbox effect"""
mask = torch.ones((height, width), device=self.device)
if box_mode == "letterbox":
# Create horizontal bars
if bar_size > 0:
# Top bar
if feather > 0:
for i in range(feather):
mask[bar_size - feather + i, :] = i / feather
mask[:bar_size - feather, :] = 0
# Bottom bar
if feather > 0:
for i in range(feather):
mask[height - bar_size + i, :] = i / feather
mask[height - bar_size + feather:, :] = 0
else: # pillarbox
# Create vertical bars
if bar_size > 0:
# Left bar
if feather > 0:
for i in range(feather):
mask[:, bar_size - feather + i] = i / feather
mask[:, :bar_size - feather] = 0
# Right bar
if feather > 0:
for i in range(feather):
mask[:, width - bar_size + i] = i / feather
mask[:, width - bar_size + feather:] = 0
return mask
def apply_black_bars(self, images: torch.Tensor,
box_mode: str,
bar_size: int,
bar_feather: int) -> Tuple[torch.Tensor]:
"""
Apply letterboxing or pillarboxing effect to a batch of images.
Args:
images: Input tensor of shape (B, H, W, C)
box_mode: Either "letterbox" or "pillarbox"
bar_size: Size of the black bars in pixels
bar_feather: Size of the feathered edge in pixels
Returns:
Tuple containing the processed images tensor
"""
# Ensure input is torch tensor on the correct device
if not isinstance(images, torch.Tensor):
images = torch.tensor(images, device=self.device)
else:
images = images.to(self.device)
# Get dimensions
B, H, W, C = images.shape
# Create the mask
mask = self.create_mask(H, W, box_mode, bar_size, bar_feather)
# Expand mask for broadcasting
mask = mask.unsqueeze(0).unsqueeze(-1).expand(B, -1, -1, C)
# Apply the mask to create the letterbox/pillarbox effect
result = images * mask
return (result,)
NODE_CLASS_MAPPINGS = {
"BlackBarsV1": BlackBarsV1
}
NODE_DISPLAY_NAME_MAPPINGS = {
"BlackBarsV1": "Black Bars V1"
}