Nodes/comfyui_CustomCodeNode/Custom Code (Image)
ComfyUI Node

Custom Code (Image)

Image math that didn't need its own node

By What-a-stupid-username·Created about a year ago·Updated about a year ago· 2
Custom Code (Image)
  • image0
  • image0
I0
F0.50
AlwaysExecutefalse
codeimage0 = image0 * F

There's a whole cottage industry of nodes that do one image operation each - a brightness node, a gamma node, a channel-mixer node. Custom Code (Image) (class CustomCodeNodeImg) is the version of that idea where you just write the operation yourself. It takes one image in, runs your Python snippet on it, and hands the result back out. If the thing you want to do to your pixels is expressible in a few lines of code, this is faster than hunting for (or writing) the dedicated node.

It's the image-specific sibling in the comfyui-CustomCodeNode pack, alongside the general Custom Code Node and the String variant. Same author, same trick: a code box that executes in the middle of your graph.

How it works

The node compiles your code and runs it with exec(), with image0 available as a local variable - a ComfyUI IMAGE, i.e. a float tensor shaped (batch, height, width, channels) with values roughly 0–1. Whatever you assign back to image0 before the snippet ends is what comes out the output socket. Fail to assign it and the node throws "Error Executing Custom Code" with a line-numbered traceback.

The default snippet is the perfect starter example:

image0 = image0 * F

That's a brightness multiply - darken the image with F below 1, blow it out above. Because the code runs in ComfyUI's real Python environment, you can import torch or import numpy and do whatever you'd do in a script: gamma curves, HSV shifts, channel swaps, blending two images, clamping, contrast stretches, even a quick image0.mean() printed to the console for debugging.

The inputs that matter

  • code - your snippet. The one input you'll actually type in.
  • image0 - the optional IMAGE input. Leave it unwired and it arrives as None, so the default snippet will crash; either wire an image or write code that handles None.
  • F (float) and I (int) - always in scope inside your snippet. F is the classic dial for "how much," right there on the node.
  • AlwaysExecute - ComfyUI caches nodes whose inputs haven't changed, so if your code is non-deterministic (noise, random), it can get skipped. This toggle forces a run every time.

Installing it

It ships in the same pack as the other two nodes, so one install gets you all three. Through ComfyUI Manager, search comfyui-CustomCodeNode; or clone it directly:

cd ComfyUI/custom_nodes
git clone https://github.com/What-a-stupid-username/comfyui-CustomCodeNode

Restart ComfyUI. No dependencies, no model downloads - it's pure Python stdlib plus whatever ComfyUI already gives you.

Common issues & troubleshooting

"Error Executing Custom Code" usually means your snippet threw - a None * F on an unwired image0, a typo, a missing import. The node prints the real traceback with the offending line and a caret to the console; read that, not the generic error bubble.

Image doesn't change when inputs do? Caching again - flip AlwaysExecute.

The output looks wrong (black/white/inverted)? You're past the range it expects. Tensor image math assumes roughly 0–1, so a multiply by 5 produces values the preview clamps. torch.clamp(image0, 0.0, 1.0) at the end of your snippet is a good habit.

It won't replace the big image-processing suites, and it isn't meant to - it's the scalpel for the one-off pixel operation that no packaged node does quite right.

CategoryCustomCodeNode

Inputs (5)

NameTypeDefaultDescription
IINT0
FFLOAT0.50
AlwaysExecuteBOOLEANfalseIf True, the node will always execute, even if the input is not changed.
codeSTRINGimage0 = image0 * F
image0optIMAGE

Outputs (1)

NameTypeDescription
image0IMAGE