単純な計算
画像のサイズを綺麗に半分にしたり、バッチサイズを調整したりと、単純な四則演算をする場面はしばしばあります。 そのためのノードを見ていきましょう。
おすすめのカスタムノード
単純な機能なので、これを実装するものは探せばいくらでもありますが、以下のどちらかのカスタムノードがあれば十分です。(コアノードとして実装されるといいんですがね…)
- ComfyUI_essentials
- 優れたノードなのですが、ほぼアーカイブ状態であるため、優れたノードが見つかれば差し替えます。
- ComfyUI-Custom-Scripts
ここでは、ComfyUI_essentialsの Simple Mathノード を例に解説します。
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
}
a, b, c にそれぞれ数値を入れられます。
その変数を利用して、a * b - c のように書けば、単純に算数を行うことができます。
また、これはPythonの式をそのまま使ったものなので、もう少し高度な計算も行えます。
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, そうでなければ0)
(a == b) * 1 # 論理式:等しいかどうか判定
(a != b) * 1 # 論理式:異なるかどうか判定
int型とfloat型
数字には「型(タイプ)」があります。
ComfyUIでは主に int と float の2種類を使います。
- int型:整数のみ(例:
512,32,1)- バッチサイズや画像の解像度など
- float型:小数を扱える(例:
0.7,1.5,24.0)- KSamplerのstrengthや動画のfpsなど
適切な型で入出力しないとノードに接続できません。 「全部floatでええやん」というツッコミが聞こえてきそうですが、計算の効率や精度のために区別されているんですね…慣れてしまいましょう。
型の変換
ちなみに、値を一度 Simple Math ノードに通すと、int ↔ float の変換を行えます。
入力がfloatでも、出力先がintなら自動的に変換してくれます。

{
"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
}
【小技】入力欄での簡易計算
ノードを使うほどでもない簡単な計算なら、入力欄で直接計算式を書くと、計算された値が入力されます。

Power Puter (rgthree)
rgthree-comfy で追加される Power Puter を使うと、画像のサイズを取得できたりif文が使えたりと、もはやほぼプログラミングですが、より複雑な処理をすることができます。

{
"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
}