saliacoel commited on
Commit
c06eb0e
·
verified ·
1 Parent(s): 579544e

Upload salia_prepare_rgba_mp4.py

Browse files
Files changed (1) hide show
  1. salia_prepare_rgba_mp4.py +107 -0
salia_prepare_rgba_mp4.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+
4
+ class Prepare_rgba_mp4:
5
+ """
6
+ Converts a batch of RGBA images into a batch of RGB images with packed alpha.
7
+
8
+ Output layout for each image:
9
+ - Top half: alpha channel converted to grayscale RGB
10
+ - Bottom half: original RGB content with alpha removed
11
+
12
+ The output keeps the same batch size and width, and doubles the height.
13
+ """
14
+
15
+ CATEGORY = "image/alpha"
16
+ FUNCTION = "pack"
17
+ RETURN_TYPES = ("IMAGE",)
18
+ RETURN_NAMES = ("images",)
19
+
20
+ @classmethod
21
+ def INPUT_TYPES(cls):
22
+ return {
23
+ "required": {
24
+ "images": ("IMAGE",),
25
+ },
26
+ "optional": {
27
+ "alpha_mask": ("MASK",),
28
+ },
29
+ }
30
+
31
+ def _normalize_mask_shape(self, alpha_mask, batch_size, height, width, device, dtype):
32
+ if alpha_mask is None:
33
+ return None
34
+
35
+ mask = alpha_mask.to(device=device, dtype=dtype)
36
+
37
+ # Accept [H,W], [B,H,W], [H,W,1], or [B,H,W,1]
38
+ if mask.ndim == 2:
39
+ mask = mask.unsqueeze(0)
40
+ elif mask.ndim == 4 and mask.shape[-1] == 1:
41
+ mask = mask.squeeze(-1)
42
+ elif mask.ndim == 3:
43
+ pass
44
+ else:
45
+ raise ValueError(
46
+ f"alpha_mask must have shape [H,W], [B,H,W], [H,W,1], or [B,H,W,1], got {tuple(mask.shape)}"
47
+ )
48
+
49
+ if mask.shape[1] != height or mask.shape[2] != width:
50
+ raise ValueError(
51
+ f"alpha_mask resolution {tuple(mask.shape[-2:])} does not match image resolution {(height, width)}"
52
+ )
53
+
54
+ if mask.shape[0] == 1 and batch_size > 1:
55
+ mask = mask.expand(batch_size, -1, -1)
56
+ elif mask.shape[0] != batch_size:
57
+ raise ValueError(
58
+ f"alpha_mask batch size {mask.shape[0]} does not match image batch size {batch_size}"
59
+ )
60
+
61
+ return mask.clamp(0.0, 1.0)
62
+
63
+ def pack(self, images, alpha_mask=None):
64
+ if images.ndim != 4:
65
+ raise ValueError(f"images must have shape [B,H,W,C], got {tuple(images.shape)}")
66
+
67
+ batch_size, height, width, channels = images.shape
68
+
69
+ if channels < 3:
70
+ raise ValueError(f"images must have at least 3 channels, got {channels}")
71
+
72
+ rgb = images[..., :3].clamp(0.0, 1.0)
73
+ mask = self._normalize_mask_shape(
74
+ alpha_mask,
75
+ batch_size=batch_size,
76
+ height=height,
77
+ width=width,
78
+ device=images.device,
79
+ dtype=images.dtype,
80
+ )
81
+
82
+ if channels >= 4:
83
+ alpha = images[..., 3].to(device=images.device, dtype=images.dtype).clamp(0.0, 1.0)
84
+ elif mask is not None:
85
+ # ComfyUI MASK from Load Image is inverted alpha: visible=0, invisible=1.
86
+ alpha = (1.0 - mask).clamp(0.0, 1.0)
87
+ else:
88
+ raise ValueError(
89
+ "No alpha channel found. Provide a 4-channel IMAGE, or connect a MASK to alpha_mask."
90
+ )
91
+
92
+ # Top half = alpha as grayscale RGB
93
+ alpha_rgb = alpha.unsqueeze(-1).expand(-1, -1, -1, 3)
94
+
95
+ # Bottom half = original RGB
96
+ packed = torch.cat((alpha_rgb, rgb), dim=1).clamp(0.0, 1.0)
97
+
98
+ return (packed,)
99
+
100
+
101
+ NODE_CLASS_MAPPINGS = {
102
+ "Prepare_rgba_mp4": Prepare_rgba_mp4,
103
+ }
104
+
105
+ NODE_DISPLAY_NAME_MAPPINGS = {
106
+ "Prepare_rgba_mp4": "Prepare_rgba_mp4 (Top greyscale alpha)",
107
+ }