Last active
February 13, 2025 03:26
-
-
Save robinjhuang/fbf54b7715091c7b478724fc4dffbd03 to your computer and use it in GitHub Desktop.
ComfyUI Image Selector Node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// web/js/imageSelector.js | |
app.registerExtension({ | |
name: "example.imageselector", | |
async setup() { | |
function messageHandler(event) { alert(event.detail.message); } | |
app.api.addEventListener("example.imageselector.textmessage", messageHandler); | |
}, | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# src/nodes.py | |
import torch | |
from server import PromptServer | |
class ImageSelector: | |
CATEGORY = "example" | |
@classmethod | |
def INPUT_TYPES(s): | |
return { "required": { | |
"images": ("IMAGE",), | |
"mode": (["brightest", "reddest", "greenest", "bluest"],) | |
}} | |
RETURN_TYPES = ("IMAGE",) | |
FUNCTION = "choose_image" | |
def choose_image(self, images, mode): | |
batch_size = images.shape[0] | |
brightness = list(torch.mean(image.flatten()).item() for image in images) | |
if (mode=="brightest"): | |
scores = brightness | |
else: | |
channel = 0 if mode=="reddest" else (1 if mode=="greenest" else 2) | |
absolute = list(torch.mean(image[:,:,channel].flatten()).item() for image in images) | |
scores = list( absolute[i]/(brightness[i]+1e-8) for i in range(batch_size) ) | |
best = scores.index(max(scores)) | |
result = images[best].unsqueeze(0) | |
PromptServer.instance.send_sync("example.imageselector.textmessage", {"message":f"Picked image {best+1}"}) | |
return (result,) | |
NODE_CLASS_MAPPINGS = { | |
"Example" : Example, | |
"Image Selector" : ImageSelector, | |
} | |
NODE_DISPLAY_NAME_MAPPINGS = { | |
"Example": "Example Node", | |
"Image Selector": "Image Selector", | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment