-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcolor_blend.py
72 lines (60 loc) · 2.44 KB
/
color_blend.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
# Color blend node by Yam Levi
# Property of Stability AI
import cv2
import numpy as np
from PIL import Image
import torch
import comfy.utils
def color_blend(bw_layer,color_layer):
# Convert the color layer to LAB color space
color_lab = cv2.cvtColor(color_layer, cv2.COLOR_BGR2Lab)
# Convert the black and white layer to grayscale
bw_layer_gray = cv2.cvtColor(bw_layer, cv2.COLOR_BGR2GRAY)
# Replace the luminosity (L) channel in the color image with the black and white luminosity
_, color_a, color_b = cv2.split(color_lab)
blended_lab = cv2.merge((bw_layer_gray, color_a, color_b))
# Convert the blended LAB image back to BGR color space
blended_result = cv2.cvtColor(blended_lab, cv2.COLOR_Lab2BGR)
return blended_result
class ColorBlend:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"bw_layer": ("IMAGE",),
"color_layer": ("IMAGE",),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "color_blending_mode"
CATEGORY = "stability/image/postprocessing"
def color_blending_mode(self, bw_layer, color_layer):
if bw_layer.shape[0] < color_layer.shape[0]:
bw_layer = bw_layer.repeat(color_layer.shape[0], 1, 1, 1)[:color_layer.shape[0]]
if bw_layer.shape[0] > color_layer.shape[0]:
color_layer = color_layer.repeat(bw_layer.shape[0], 1, 1, 1)[:bw_layer.shape[0]]
batch_size, height, width, _ = bw_layer.shape
tensor_output = torch.empty_like(bw_layer)
image1 = bw_layer.cpu()
image2 = color_layer.cpu()
if image1.shape != image2.shape:
#print(image1.shape)
#print(image2.shape)
image2 = image2.permute(0, 3, 1, 2)
image2 = comfy.utils.common_upscale(image2, image1.shape[2], image1.shape[1], upscale_method='bicubic', crop='center')
image2 = image2.permute(0, 2, 3, 1)
image1 = (image1 * 255).to(torch.uint8).numpy()
image2 = (image2 * 255).to(torch.uint8).numpy()
for i in range(batch_size):
blend = color_blend(image1[i],image2[i])
blend = np.stack([blend])
tensor_output[i:i+1] = (torch.from_numpy(blend.transpose(0, 3, 1, 2))/255.0).permute(0, 2, 3, 1)
return (tensor_output,)
NODE_CLASS_MAPPINGS = {
"ColorBlend": ColorBlend
}
NODE_DISPLAY_NAME_MAPPINGS = {
"ColorBlend": "Color Blend"
}