add resolution printout

This commit is contained in:
Tropfchen
2024-01-20 15:14:52 +01:00
parent 68ee689fa3
commit 0d8acdad86
3 changed files with 61 additions and 6 deletions
+5
View File
@@ -1,3 +1,8 @@
# pre-commit
.flake8
.pre-commit-config.yaml
pyproject.toml
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
+36 -2
View File
@@ -1,4 +1,5 @@
import { app } from "../../scripts/app.js";
import { ComfyWidgets } from "/scripts/widgets.js";
// ----------- ComfyUI\web\extensions\core\widgetInputs.js copypaste -----------
const CONVERTED_TYPE = "converted-widget";
@@ -139,9 +140,42 @@ function prependNewNode(node, nodeData, newNodeName, newNodewidgetNames) {
app.registerExtension({
name: "trop.YARS",
async beforeRegisterNodeDef(nodeType, nodeData, app) {
if (
// add resolution printout
if (nodeData.name === "YARSAdv" || nodeData.name === "YARS") {
const onExecuted = nodeType.prototype.onExecuted;
nodeType.prototype.onExecuted = function (message) {
const r = onExecuted?.apply?.(this, arguments);
let w = this.widgets.find((w) => w.name === "resolution_printout");
if (!w) {
w = ComfyWidgets["STRING"](
this,
"resolution_printout",
["STRING", { multiline: true }],
app,
).widget;
w.inputEl.readOnly = true;
w.inputEl.style.opacity = 0.8;
}
const width = message.width[0];
const height = message.height[0];
const mpx = ((width * height) / 1048576).toFixed(2);
const res_msg = `resolution: ${width}x${height} (~${mpx} Mpx)`;
const ratio_msg = `ratio: ~${message.ratio[0].toFixed(2)}`;
w.value = `${res_msg}\n${ratio_msg}`;
this.onResize?.(this.size);
return r;
};
}
// add quick nodes
else if (
nodeData.name === "EmptyLatentImage" ||
nodeData.name === "ImageScale"
nodeData.name === "ImageScale" ||
nodeData.name === "LatentUpscale"
) {
addMenuHandler(nodeType, function (_, options) {
const entries = [
+20 -4
View File
@@ -106,12 +106,20 @@ class YARS:
base_resolution: int,
aspect_ratio: str,
overextend: bool,
) -> tuple[int, int]:
):
if m := re.search(r"(\d+):(\d+)", aspect_ratio):
ratio: float = int(m.group(2)) / int(m.group(1))
d = calculate_aspect_ratio(base_resolution, ratio, overextend)
return (d.width, d.height)
# return as dict with `ui` key to trigger onExecuted
return {
"ui": {
"width": [d.width],
"height": [d.height],
"ratio": [ratio],
},
"result": (d.width, d.height),
}
raise ValueError(f"Could't find aspect ratio in string `{aspect_ratio}`")
@@ -175,7 +183,7 @@ class YARSAdv:
height_ratio: int,
overextend: bool,
constant_resolution: bool,
) -> tuple[int, int]:
):
ratio: float = height_ratio / width_ratio
if constant_resolution:
@@ -183,7 +191,15 @@ class YARSAdv:
else:
d = calculate_aspect_ratio(base_resolution, ratio, overextend)
return (d.width, d.height)
# return as dict with `ui` key to trigger onExecuted
return {
"ui": {
"width": [d.width],
"height": [d.height],
"ratio": [width_ratio / height_ratio],
},
"result": (d.width, d.height),
}
if __name__ == "__main__":