This commit is contained in:
shadowcz007
2024-06-16 11:52:09 +08:00
parent 30c323216b
commit e53f6ed810
2 changed files with 48 additions and 58 deletions
+29 -10
View File
@@ -12,6 +12,19 @@ import hashlib
def tensor2pil(image):
return Image.fromarray(np.clip(255. * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8))
# tensor 取hash值
def tensor_to_hash(tensor):
# 将 Tensor 转换为 NumPy 数组
np_array = tensor.cpu().numpy()
# 将 NumPy 数组转换为字节数据
byte_data = np_array.tobytes()
# 计算哈希值
hash_value = hashlib.md5(byte_data).hexdigest()
return hash_value
def create_temp_file(image):
output_dir = folder_paths.get_temp_directory()
@@ -43,6 +56,10 @@ def create_temp_file(image):
# image - tensor - 文件路径
# loadImage的方法( 文件路径 - image-mask )
class EditMask:
def __init__(self):
self.image_id = None
@classmethod
def INPUT_TYPES(s):
return {"required":
@@ -67,8 +84,19 @@ class EditMask:
def edit(self, image,image_update=None):
# 根据image输入来判断是否是新的图片
if self.image_id==None:
self.image_id=tensor_to_hash(image)
image_update=None
else:
image_id=tensor_to_hash(image)
if image_id!=self.image_id:
image_update=None
self.image_id=image_id
image_path=None
print('#image_update',image_update)
# print('#image_update',self.image_id,image_update)
if image_update==None:
print('--')
else:
@@ -142,12 +170,3 @@ class EditMask:
return {"ui":{"images": images},"result": (output_image, output_mask)}
# return (output_image, output_mask)
@classmethod
def IS_CHANGED(s, image):
image_path = folder_paths.get_annotated_filepath(image)
m = hashlib.sha256()
with open(image_path, 'rb') as f:
m.update(f.read())
print('IS_CHANGED',image_path)
return m.digest().hex()
+19 -48
View File
@@ -1,57 +1,26 @@
import { app } from '../../../scripts/app.js'
import { ComfyDialog } from '../../../scripts/ui.js'
// import { ComfyDialog } from '../../../scripts/ui.js'
import { api } from '../../../scripts/api.js'
// 扩展原型链上的 close 方法
const originalClose = ComfyDialog.prototype.close
ComfyDialog.prototype.close = function () {
// console.log('#ComfyDialog', 111111) // 新增的console日志
originalClose.call(this) // 调用原始的 close 方法
// const originalClose = ComfyDialog.prototype.close
// ComfyDialog.prototype.close = function () {
// // console.log('#ComfyDialog', 111111) // 新增的console日志
// originalClose.call(this) // 调用原始的 close 方法
const nodes = app.graph.findNodesByType('EditMask')
// const nodes = app.graph.findNodesByType('EditMask')
for (const node of nodes) {
const image_update = node.widgets.filter(w => w.name == 'image_update')[0]
//为了做一个mask编辑后的标记
node.images=Array.from(node.images,im=>{
im.update=true;
return im
})
image_update.value = { images:node.images }
}
}
// for (const node of nodes) {
// const image_update = node.widgets.filter(w => w.name == 'image_update')[0]
// //为了做一个mask编辑后的标记
// node.images=Array.from(node.images,im=>{
// im.update=true;
// return im
// })
// image_update.value = { images:node.images }
// }
// }
function get_position_style (ctx, widget_width, y, node_height) {
const MARGIN = 4 // the margin around the html element
/* Create a transform that deals with all the scrolling and zooming */
const elRect = ctx.canvas.getBoundingClientRect()
const transform = new DOMMatrix()
.scaleSelf(
elRect.width / ctx.canvas.width,
elRect.height / ctx.canvas.height
)
.multiplySelf(ctx.getTransform())
.translateSelf(MARGIN, MARGIN + y)
return {
transformOrigin: '0 0',
transform: transform,
left: `0`,
top: '0',
cursor: 'pointer',
position: 'absolute',
maxWidth: `${widget_width - MARGIN * 2}px`,
// maxHeight: `${node_height - MARGIN * 2}px`, // we're assuming we have the whole height of the node
width: `${widget_width - MARGIN * 2}px`,
// height: `${node_height * 0.3 - MARGIN * 2}px`,
// background: '#EEEEEE',
display: 'flex',
flexDirection: 'column',
// alignItems: 'center',
justifyContent: 'space-around'
}
}
app.registerExtension({
name: 'comfyui-edit-mask.editMask',
@@ -65,7 +34,7 @@ app.registerExtension({
draw (ctx, node, width, y) {},
callback: e => console.log(e),
computeSize (...args) {
return [128, 88] // a method to compute the current size of the widget
return [128, 24] // a method to compute the current size of the widget
},
async serializeValue (nodeId, widgetIndex) {
// node=app.graph.getNodeById(node.id)
@@ -109,6 +78,8 @@ app.registerExtension({
if (image_update.value?.images?.length > 0)
showImage(image_update.value.images[0])
// 数据要写入到节点
node.images=image_update.value.images;
}
}
})