Mask canvas for masked img2img
This commit is contained in:
@@ -1,8 +1,17 @@
|
||||
import { LiteGraph, type ITextWidget, type SlotLayout, type INumberWidget } from "@litegraph-ts/core";
|
||||
import ComfyGraphNode from "./ComfyGraphNode";
|
||||
import ComfyGraphNode, { type ComfyGraphNodeProperties } from "./ComfyGraphNode";
|
||||
import { comfyFileToAnnotatedFilepath, type ComfyBoxImageMetadata } from "$lib/utils";
|
||||
|
||||
export interface ComfyPickImageProperties extends ComfyGraphNodeProperties {
|
||||
imageTagFilter: string
|
||||
}
|
||||
|
||||
export default class ComfyPickImageNode extends ComfyGraphNode {
|
||||
override properties: ComfyPickImageProperties = {
|
||||
tags: [],
|
||||
imageTagFilter: ""
|
||||
}
|
||||
|
||||
static slotLayout: SlotLayout = {
|
||||
inputs: [
|
||||
{ name: "images", type: "COMFYBOX_IMAGES,COMFYBOX_IMAGE" },
|
||||
@@ -13,57 +22,87 @@ export default class ComfyPickImageNode extends ComfyGraphNode {
|
||||
{ name: "filename", type: "string" },
|
||||
{ name: "width", type: "number" },
|
||||
{ name: "height", type: "number" },
|
||||
{ name: "children", type: "COMFYBOX_IMAGES" },
|
||||
]
|
||||
}
|
||||
|
||||
tagFilterWidget: ITextWidget;
|
||||
filepathWidget: ITextWidget;
|
||||
folderWidget: ITextWidget;
|
||||
widthWidget: INumberWidget;
|
||||
heightWidget: INumberWidget;
|
||||
tagsWidget: ITextWidget;
|
||||
childrenWidget: INumberWidget;
|
||||
|
||||
constructor(title?: string) {
|
||||
super(title)
|
||||
this.tagFilterWidget = this.addWidget("text", "Tag Filter", this.properties.imageTagFilter, "imageTagFilter")
|
||||
|
||||
this.filepathWidget = this.addWidget("text", "File", "")
|
||||
this.filepathWidget.disabled = true;
|
||||
this.folderWidget = this.addWidget("text", "Folder", "")
|
||||
this.folderWidget.disabled = true;
|
||||
this.widthWidget = this.addWidget("number", "Width", 0)
|
||||
this.widthWidget.disabled = true;
|
||||
this.heightWidget = this.addWidget("number", "Height", 0)
|
||||
for (const widget of this.widgets)
|
||||
widget.disabled = true;
|
||||
this.heightWidget.disabled = true;
|
||||
this.tagsWidget = this.addWidget("text", "Tags", "")
|
||||
this.tagsWidget.disabled = true;
|
||||
this.childrenWidget = this.addWidget("number", "# of Children", 0)
|
||||
this.childrenWidget.disabled = true;
|
||||
}
|
||||
|
||||
_value: ComfyBoxImageMetadata[] | null = null;
|
||||
_image: ComfyBoxImageMetadata | null = null;
|
||||
_path: string | null = null;
|
||||
_index: number = 0;
|
||||
_index: number | null = null;
|
||||
|
||||
private setValue(value: ComfyBoxImageMetadata[] | ComfyBoxImageMetadata | null, index: number) {
|
||||
if (value != null && !Array.isArray(value)) {
|
||||
value = [value]
|
||||
index = 0;
|
||||
}
|
||||
const changed = this._value != value || this._index != index;
|
||||
this._value = value as ComfyBoxImageMetadata[];
|
||||
this._index = index;
|
||||
let image: ComfyBoxImageMetadata | null = null;
|
||||
if (value && this._index != null && value[this._index] != null) {
|
||||
image = value[this._index];
|
||||
}
|
||||
|
||||
const changed = this._value != value || this._index != index || this._image != image;
|
||||
|
||||
if (changed) {
|
||||
if (value && value[this._index] != null) {
|
||||
this._image = value[this._index]
|
||||
if (image) {
|
||||
this._image = image
|
||||
this._image.children ||= []
|
||||
this._image.tags ||= []
|
||||
|
||||
this._path = comfyFileToAnnotatedFilepath(this._image.comfyUIFile);
|
||||
this.filepathWidget.value = this._image.comfyUIFile.filename
|
||||
this.folderWidget.value = this._image.comfyUIFile.type
|
||||
this.childrenWidget.value = this._image.children.length
|
||||
this.tagsWidget.value = this._image.tags.join(", ")
|
||||
}
|
||||
else {
|
||||
this._image = null;
|
||||
this._path = null;
|
||||
this.filepathWidget.value = "(None)"
|
||||
this.folderWidget.value = ""
|
||||
this.childrenWidget.value = 0
|
||||
this.tagsWidget.value = ""
|
||||
}
|
||||
console.log("SET", value, this._image, this._path)
|
||||
|
||||
console.log("SET", value, this._image, this._path, this.properties.imageTagFilter)
|
||||
}
|
||||
}
|
||||
|
||||
override onExecute() {
|
||||
const data = this.getInputData(0)
|
||||
const index = this.getInputData(1) || 0
|
||||
let index = this.getInputData(1);
|
||||
if (this.properties.imageTagFilter && Array.isArray(data))
|
||||
index = data.findIndex(i => i.tags?.includes(this.properties.imageTagFilter))
|
||||
else
|
||||
index = 0;
|
||||
this.setValue(data, index);
|
||||
|
||||
if (this._image == null) {
|
||||
@@ -71,6 +110,7 @@ export default class ComfyPickImageNode extends ComfyGraphNode {
|
||||
this.setOutputData(1, null)
|
||||
this.setOutputData(2, 0)
|
||||
this.setOutputData(3, 0)
|
||||
this.setOutputData(4, null)
|
||||
|
||||
this.widthWidget.value = 0
|
||||
this.heightWidget.value = 0
|
||||
@@ -80,6 +120,7 @@ export default class ComfyPickImageNode extends ComfyGraphNode {
|
||||
this.setOutputData(1, this._path);
|
||||
this.setOutputData(2, this._image.width);
|
||||
this.setOutputData(3, this._image.height);
|
||||
this.setOutputData(4, this._image.children);
|
||||
|
||||
// XXX: image size doesn't load until the <img> element is ready on
|
||||
// the page so this can come after several frames' worth of
|
||||
|
||||
@@ -8,12 +8,14 @@ import ComfyWidgetNode from "./ComfyWidgetNode";
|
||||
import { get, writable, type Writable } from "svelte/store";
|
||||
|
||||
export interface ComfyImageUploadNodeProperties extends ComfyWidgetProperties {
|
||||
maskCount: number
|
||||
}
|
||||
|
||||
export default class ComfyImageUploadNode extends ComfyWidgetNode<ComfyBoxImageMetadata[]> {
|
||||
properties: ComfyImageUploadNodeProperties = {
|
||||
defaultValue: [],
|
||||
tags: [],
|
||||
maskCount: 0
|
||||
}
|
||||
|
||||
static slotLayout: SlotLayout = {
|
||||
@@ -39,13 +41,21 @@ export default class ComfyImageUploadNode extends ComfyWidgetNode<ComfyBoxImageM
|
||||
super(name, [])
|
||||
}
|
||||
|
||||
override onExecute() {
|
||||
override onExecute(param: any, options: object) {
|
||||
// TODO better way of getting image size?
|
||||
const value = get(this.value)
|
||||
if (value && value.length > 0) {
|
||||
value[0].width = get(this.imgWidth)
|
||||
value[0].height = get(this.imgHeight)
|
||||
|
||||
// NOTE: assumes masks will have the same image size as the parent image!
|
||||
for (const child of value[0].children) {
|
||||
child.width = get(this.imgWidth)
|
||||
child.height = get(this.imgHeight)
|
||||
}
|
||||
}
|
||||
|
||||
super.onExecute(param, options);
|
||||
}
|
||||
|
||||
override parseValue(value: any): ComfyBoxImageMetadata[] {
|
||||
|
||||
Reference in New Issue
Block a user