comfyanonymous / ComfyUI

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface.
https://www.comfy.org/
GNU General Public License v3.0
56.01k stars 5.92k forks source link

Request/suggestion: Adding more blend modes to Image Blend (Luminosity and Colorize) #2444

Open vitacon opened 10 months ago

vitacon commented 10 months ago

I use ComfyUI to colorize photos and to get a better result I need to combine Hue and Saturation of the output with the Luminance of the input image. So far I do it with an .exe made in .NET but it would be much nicer to handle it in ComfyUI.

It should be rather easy but I don't know Python libraries. I was pointed to kornia (K.color.rgb_to_hls) but it seems it's not included within Comfy because the code below does not work.

nodes_post_processing.py

        elif mode == "difference":
            return img1 - img2
        elif mode == "luminosity":
            img1_hls = K.color.rgb_to_hls(img1)
            img2_hls = K.color.rgb_to_hls(img2)
            combined_image_hsl = torch.cat([img1_hls[:, :1, :, :], img2_hls[:, 1:, :, :]], dim=1)
            combined_image_rgb = K.color.hls_to_rgb(combined_image_hsl)
            return(combined_image_rgb)
        else:
            raise ValueError(f"Unsupported blend mode: {mode}")

What is the easiest way to do it?

vitacon commented 10 months ago

I took me a while to notice it is already solved by a custom node. https://github.com/Stability-AI/stability-ComfyUI-nodes

And it is handled using cv2.

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
ducha-aiki commented 7 months ago

kornia now is installed by default https://github.com/comfyanonymous/ComfyUI/blob/master/requirements.txt#L13 so the original code snippet should work now