-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathremove_transparency.py
47 lines (39 loc) · 1.56 KB
/
remove_transparency.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
import torch
class RemoveTransparency:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"recover_background": ("BOOLEAN", {"default": False}),
"background_color": (["black", "white", "greenscreen"], {"default": "black"}),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "process_transparency"
CATEGORY = "Bjornulf"
def process_transparency(self, image, recover_background, background_color):
# Check if the image has an alpha channel
if image.shape[3] == 4:
rgb = image[:, :, :, :3]
alpha = image[:, :, :, 3:4]
if recover_background:
result = rgb
else:
# Create background color tensor
if background_color == "white":
bg_color = torch.ones_like(rgb)
elif background_color == "greenscreen":
bg_color = torch.zeros_like(rgb)
bg_color[:, :, :, 1] = 1 # Set green channel to 1
else: # black
bg_color = torch.zeros_like(rgb)
# Blend the image with the background color
result = rgb * alpha + bg_color * (1 - alpha)
else:
# If there's no alpha channel, return the original image
result = image
# Ensure the output is always 3 channels (RGB)
if result.shape[3] == 4:
result = result[:, :, :, :3]
return (result,)