Convert Image To Palette In ComfyUI: A Step-by-Step Guide
Are you looking to streamline your color palette creation in ComfyUI? Manually typing in color codes can be tedious and time-consuming. This guide introduces a simple yet powerful method to convert images into color palettes within ComfyUI, leveraging a custom node that automates the process. This method is particularly useful for designers, artists, and anyone working with color in digital art and design.
Why Convert Images to Color Palettes?
In the realm of digital art and design, color palettes play a pivotal role in setting the mood, conveying emotions, and ensuring visual consistency. Extracting a color palette from an image can be incredibly useful for various reasons:
- Inspiration: An image can serve as a rich source of color inspiration, providing a harmonious set of colors that work well together.
- Consistency: When working on a project, using a consistent color palette helps maintain a cohesive visual identity.
- Efficiency: Manually picking colors can be time-consuming. Converting an image to a color palette streamlines the process, allowing you to quickly access and use the colors you need.
- Experimentation: Easily generating palettes from different images allows for quick experimentation with various color schemes.
This method simplifies the process, making it easier to incorporate your desired color schemes into your projects.
Introducing the ImageToColorPaletteNode
To address the cumbersome process of manually inputting colors, the ImageToColorPaletteNode was developed. This custom node for ComfyUI automatically extracts all unique colors from an image and converts them into a COLOR_LIST. This is particularly beneficial when working with palette images or when you want to reduce the number of colors in your project.
Key Features:
- Automated Color Extraction: The node automatically identifies and extracts all unique colors from an image.
COLOR_LISTConversion: Converts the extracted colors into aCOLOR_LISTformat, making them readily usable within ComfyUI.- Efficiency: Saves time and effort by eliminating the need to manually input color codes.
- Versatility: Works well with palette images and can be used to generate color palettes for various projects.
By using this node, you can significantly speed up your workflow and ensure color consistency across your designs.
Step-by-Step Implementation
To implement the ImageToColorPaletteNode, follow these steps:
1. Add the Code to color_transfer.py
First, you need to add the Python code for the node to your color_transfer.py file. This file likely resides within your ComfyUI custom nodes directory. Open color_transfer.py and add the following code snippet:
class ImageToColorPaletteNode(ComfyNodeABC):
@classmethod
def INPUT_TYPES(cls) -> dict:
return {
"required": {
"image": (IO.IMAGE,),
}
}
RETURN_TYPES = ("COLOR_LIST",)
RETURN_NAMES = ("Color palette",)
FUNCTION = "extract_palette"
CATEGORY = "Color Transfer/Palette Transfer"
def extract_palette(
self, image: torch.Tensor
) -> tuple[list[tuple[int, int, int]]]:
"""
Converts all images in a batch to a unique color palette.
Each color is an integer RGB tuple.
"""
all_colors = set()
for img_tensor in image:
img_np = img_tensor.cpu().numpy()
img_uint8 = (img_np * 255).astype(np.uint8)
pixels = img_uint8.reshape(-1, 3)
for pixel in pixels:
all_colors.add(tuple(int(c) for c in pixel))
# Convert set to sorted list for consistency
unique_colors = sorted(all_colors)
return (unique_colors,)
This code defines the ImageToColorPaletteNode class, which includes:
INPUT_TYPES: Specifies that the node requires an image input.RETURN_TYPES: Indicates that the node returns aCOLOR_LIST.RETURN_NAMES: Names the output as "Color palette".FUNCTION: Assigns theextract_palettemethod to handle the node's functionality.CATEGORY: Categorizes the node under "Color Transfer/Palette Transfer".extract_palette: This method processes the input image, extracts unique colors, and returns them as a sorted list of RGB tuples. It iterates through each image in the batch, converts it to a NumPy array, reshapes it to a list of pixels, and adds each unique color to a set. The set is then converted to a sorted list to ensure consistency.
2. Add the Node to __init__.py
Next, you need to register the ImageToColorPaletteNode in the __init__.py file of your custom nodes directory. This file tells ComfyUI which nodes to load. Open __init__.py and add the following lines:
from .color_transfer import (..., ImageToColorPaletteNode)
...
NODE_CLASS_MAPPINGS = {
...
"ImageToPalette": ImageToColorPaletteNode,
}
This code imports the ImageToColorPaletteNode from color_transfer.py and adds it to the NODE_CLASS_MAPPINGS dictionary. The key `