Simple Math
There are often situations where simple arithmetic operations are performed, such as neatly halving the image size or adjusting the batch size.
Let's look at the nodes for that.
Recommended Custom Nodes
Since it is a simple function, there are as many things that implement this as you look for, but one of the following custom nodes is sufficient. (I hope it will be implemented as a core node...)
Here, we will explain using the Simple Math node of ComfyUI_essentials as an example.
Simple Math
{
"id": "d7badf9d-4187-470d-a5b2-a2c4296f9f8e",
"revision": 0,
"last_node_id": 16,
"last_link_id": 11,
"nodes": [
{
"id": 4,
"type": "PrimitiveInt",
"pos": [
637.447265625,
501.5946960449219
],
"size": [
210,
82
],
"flags": {},
"order": 0,
"mode": 0,
"inputs": [],
"outputs": [
{
"name": "INT",
"type": "INT",
"links": [
1
]
}
],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.56",
"Node name for S&R": "PrimitiveInt"
},
"widgets_values": [
25,
"fixed"
]
},
{
"id": 13,
"type": "PrimitiveInt",
"pos": [
637.447265625,
650.788330078125
],
"size": [
210,
82
],
"flags": {},
"order": 1,
"mode": 0,
"inputs": [],
"outputs": [
{
"name": "INT",
"type": "INT",
"links": [
10
]
}
],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.56",
"Node name for S&R": "PrimitiveInt"
},
"widgets_values": [
7,
"fixed"
]
},
{
"id": 14,
"type": "PrimitiveInt",
"pos": [
637.447265625,
799.9819946289062
],
"size": [
210,
82
],
"flags": {},
"order": 2,
"mode": 0,
"inputs": [],
"outputs": [
{
"name": "INT",
"type": "INT",
"links": [
11
]
}
],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.56",
"Node name for S&R": "PrimitiveInt"
},
"widgets_values": [
100,
"fixed"
]
},
{
"id": 3,
"type": "SimpleMath+",
"pos": [
899.1307983398438,
630.86669921875
],
"size": [
210,
98
],
"flags": {},
"order": 3,
"mode": 0,
"inputs": [
{
"name": "a",
"shape": 7,
"type": "*",
"link": 1
},
{
"name": "b",
"shape": 7,
"type": "*",
"link": 10
},
{
"name": "c",
"shape": 7,
"type": "*",
"link": 11
}
],
"outputs": [
{
"name": "INT",
"type": "INT",
"links": [
5
]
},
{
"name": "FLOAT",
"type": "FLOAT",
"links": []
}
],
"properties": {
"cnr_id": "comfyui_essentials",
"ver": "9d9f4bedfc9f0321c19faf71855e228c93bd0dc9",
"Node name for S&R": "SimpleMath+"
},
"widgets_values": [
"a * b - c"
],
"color": "#232",
"bgcolor": "#353"
},
{
"id": 8,
"type": "PreviewAny",
"pos": [
1160.396240234375,
630.86669921875
],
"size": [
210,
88
],
"flags": {},
"order": 4,
"mode": 0,
"inputs": [
{
"name": "source",
"type": "*",
"link": 5
}
],
"outputs": [],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.56",
"Node name for S&R": "PreviewAny"
},
"widgets_values": []
}
],
"links": [
[
1,
4,
0,
3,
0,
"*"
],
[
5,
3,
0,
8,
0,
"*"
],
[
10,
13,
0,
3,
1,
"*"
],
[
11,
14,
0,
3,
2,
"*"
]
],
"groups": [],
"config": {},
"extra": {
"ds": {
"scale": 1.2100000000000004,
"offset": [
-537.447265625,
-402.4211423259136
]
},
"frontendVersion": "1.26.8",
"VHS_latentpreview": false,
"VHS_latentpreviewrate": 0,
"VHS_MetadataImage": true,
"VHS_KeepIntermediate": true
},
"version": 0.4
}
You can enter numbers in a, b, and c respectively.
Using those variables, if you write like a * b - c, you can simply perform arithmetic.
Also, since this uses Python expressions as is, you can perform slightly more advanced calculations.
a // b
a % b
a ** b
( a + b) * c
abs ( a - b)
min ( a, b)
max ( a, b)
round ( a / b)
( a > b) * 1
( a == b) * 1
( a != b) * 1
int type and float type
Numbers have "types".
ComfyUI mainly uses two types: int and float .
int type : Integers only (e.g., 512, 32, 1)
Batch size, image resolution, etc.
float type : Can handle decimals (e.g., 0.7, 1.5, 24.0)
KSampler strength, video fps, etc.
You cannot connect to nodes unless you input/output with the appropriate type.
I can hear a tsukkomi saying "Everything is fine with float", but they are distinguished for calculation efficiency and precision... Let's get used to it.
Type Conversion
By the way, if you pass the value through the Simple Math node once, you can convert between int ↔ float.
Even if the input is float, if the output destination is int, it will automatically convert it.
Simple_Math_FloatInt.json
{
"id": "d7badf9d-4187-470d-a5b2-a2c4296f9f8e",
"revision": 0,
"last_node_id": 24,
"last_link_id": 20,
"nodes": [
{
"id": 21,
"type": "PrimitiveFloat",
"pos": [
651.47802734375,
628.9332275390625
],
"size": [
210,
58
],
"flags": {},
"order": 0,
"mode": 0,
"inputs": [],
"outputs": [
{
"name": "FLOAT",
"type": "FLOAT",
"links": [
18
]
}
],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.56",
"Node name for S&R": "PrimitiveFloat"
},
"widgets_values": [
12.3
]
},
{
"id": 8,
"type": "PreviewAny",
"pos": [
1160.396240234375,
628.9332275390625
],
"size": [
210,
88
],
"flags": {},
"order": 4,
"mode": 0,
"inputs": [
{
"name": "source",
"type": "*",
"link": 5
}
],
"outputs": [],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.56",
"Node name for S&R": "PreviewAny"
},
"widgets_values": []
},
{
"id": 24,
"type": "PreviewAny",
"pos": [
1162.296630859375,
815.1841430664062
],
"size": [
210,
88
],
"flags": {},
"order": 5,
"mode": 0,
"inputs": [
{
"name": "source",
"type": "*",
"link": 20
}
],
"outputs": [],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.56",
"Node name for S&R": "PreviewAny"
},
"widgets_values": []
},
{
"id": 22,
"type": "PrimitiveInt",
"pos": [
651.47802734375,
815.1841430664062
],
"size": [
210,
82
],
"flags": {},
"order": 1,
"mode": 0,
"inputs": [],
"outputs": [
{
"name": "INT",
"type": "INT",
"links": [
19
]
}
],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.56",
"Node name for S&R": "PrimitiveInt"
},
"widgets_values": [
999,
"fixed"
]
},
{
"id": 23,
"type": "SimpleMath+",
"pos": [
906.8873291015625,
815.1841430664062
],
"size": [
210,
98
],
"flags": {},
"order": 3,
"mode": 0,
"inputs": [
{
"name": "a",
"shape": 7,
"type": "*",
"link": 19
},
{
"name": "b",
"shape": 7,
"type": "*",
"link": null
},
{
"name": "c",
"shape": 7,
"type": "*",
"link": null
}
],
"outputs": [
{
"name": "INT",
"type": "INT",
"links": []
},
{
"name": "FLOAT",
"type": "FLOAT",
"links": [
20
]
}
],
"properties": {
"cnr_id": "comfyui_essentials",
"ver": "9d9f4bedfc9f0321c19faf71855e228c93bd0dc9",
"Node name for S&R": "SimpleMath+"
},
"widgets_values": [
"a"
],
"color": "#432",
"bgcolor": "#653"
},
{
"id": 3,
"type": "SimpleMath+",
"pos": [
905.9371337890625,
628.9332275390625
],
"size": [
210,
98
],
"flags": {},
"order": 2,
"mode": 0,
"inputs": [
{
"name": "a",
"shape": 7,
"type": "*",
"link": 18
},
{
"name": "b",
"shape": 7,
"type": "*",
"link": null
},
{
"name": "c",
"shape": 7,
"type": "*",
"link": null
}
],
"outputs": [
{
"name": "INT",
"type": "INT",
"links": [
5
]
},
{
"name": "FLOAT",
"type": "FLOAT",
"links": []
}
],
"properties": {
"cnr_id": "comfyui_essentials",
"ver": "9d9f4bedfc9f0321c19faf71855e228c93bd0dc9",
"Node name for S&R": "SimpleMath+"
},
"widgets_values": [
"a"
],
"color": "#323",
"bgcolor": "#535"
}
],
"links": [
[
5,
3,
0,
8,
0,
"*"
],
[
18,
21,
0,
3,
0,
"*"
],
[
19,
22,
0,
23,
0,
"*"
],
[
20,
23,
1,
24,
0,
"*"
]
],
"groups": [],
"config": {},
"extra": {
"ds": {
"scale": 1.2100000000000006,
"offset": [
-551.47802734375,
-528.9332275390625
]
},
"frontendVersion": "1.26.8",
"VHS_latentpreview": false,
"VHS_latentpreviewrate": 0,
"VHS_MetadataImage": true,
"VHS_KeepIntermediate": true
},
"version": 0.4
}
[Tip] Simple calculation in input field
If it is a simple calculation that does not worth using a node, if you write the calculation formula directly in the input field, the calculated value will be input.
Power Puter (rgthree)
If you use Power Puter added by rgthree-comfy , you can get image size or use if statements, which is almost programming, but you can perform more complex processing.
cf. Node: Power Puter (Wiki)
{
"id": "c05fc5ca-84cc-4a12-9e1a-3dbddf62dcde",
"revision": 0,
"last_node_id": 7,
"last_link_id": 6,
"nodes": [
{
"id": 3,
"type": "LoadImage",
"pos": [
3602.8349609375,
-692.7393798828125
],
"size": [
214.080078125,
314
],
"flags": {},
"order": 0,
"mode": 0,
"inputs": [],
"outputs": [
{
"name": "IMAGE",
"type": "IMAGE",
"links": [
1
]
},
{
"name": "MASK",
"type": "MASK",
"links": null
}
],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.59",
"Node name for S&R": "LoadImage"
},
"widgets_values": [
"example.png",
"image"
]
},
{
"id": 4,
"type": "PrimitiveInt",
"pos": [
3610.344970703125,
-312.99334716796875
],
"size": [
210,
82
],
"flags": {},
"order": 1,
"mode": 0,
"inputs": [],
"outputs": [
{
"name": "INT",
"type": "INT",
"links": [
2
]
}
],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.59",
"Node name for S&R": "PrimitiveInt"
},
"widgets_values": [
512,
"fixed"
]
},
{
"id": 5,
"type": "PreviewAny",
"pos": [
4196.6748046875,
-692.7393798828125
],
"size": [
210,
88
],
"flags": {},
"order": 3,
"mode": 0,
"inputs": [
{
"name": "source",
"type": "*",
"link": 3
}
],
"outputs": [],
"properties": {
"cnr_id": "comfy-core",
"ver": "0.3.59",
"Node name for S&R": "PreviewAny"
},
"widgets_values": []
},
{
"id": 1,
"type": "Power Puter (rgthree)",
"pos": [
3866.253662109375,
-692.7393798828125
],
"size": [
281.0826721191406,
259.6524963378906
],
"flags": {},
"order": 2,
"mode": 0,
"inputs": [
{
"name": "a",
"type": "*",
"link": 1
},
{
"name": "b",
"type": "*",
"link": 2
},
{
"name": "c",
"type": "*",
"link": null
}
],
"outputs": [
{
"label": "STRING",
"name": "*",
"shape": 3,
"type": "STRING",
"links": [
3
]
}
],
"properties": {
"cnr_id": "rgthree-comfy",
"ver": "0fb1e239a903e93ef626a8c20589b38f46e39dff"
},
"widgets_values": [
{
"outputs": [
"STRING"
]
},
"# テンソルの形状から画像サイズを取得\n_, height, width, _ = a.shape\n\n# 条件に応じたメッセージを作成\nif width < b:\n result = f\"This image is smaller than {b}\"\nelse:\n result = f\"This image is larger than {b}\"\n\n# 結果を出力\nprint(result)\n\n# 結果を返す\nreturn result"
],
"color": "#232",
"bgcolor": "#353"
}
],
"links": [
[
1,
3,
0,
1,
0,
"*"
],
[
2,
4,
0,
1,
1,
"*"
],
[
3,
1,
0,
5,
0,
"*"
]
],
"groups": [],
"config": {},
"extra": {
"ds": {
"scale": 1.2284597357368363,
"offset": [
-3502.8349609375,
792.7393798828125
]
},
"frontendVersion": "1.27.3",
"VHS_latentpreview": false,
"VHS_latentpreviewrate": 0,
"VHS_MetadataImage": true,
"VHS_KeepIntermediate": true
},
"version": 0.4
}