Skip to content

Commit

Permalink
v0.14.0
Browse files Browse the repository at this point in the history
发布新节点splitImage & gridoutput,用于分割图片和随机摆放元素
修复若干bug
  • Loading branch information
shadowcz007 committed Jan 20, 2024
1 parent c5e521c commit 8128796
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 79 deletions.
4 changes: 2 additions & 2 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,14 +551,14 @@ def new_add_routes(self):

# 导入节点
from .nodes.PromptNode import EmbeddingPrompt,RandomPrompt,PromptSlide,PromptSimplification,PromptImage,JoinWithDelimiter
from .nodes.ImageNode import SplitImage,GridOutput,GetImageSize_,MirroredImage,ImageColorTransfer,NoiseImage,TransparentImage,GradientImage,LoadImagesFromPath,LoadImagesFromURL,ResizeImage,TextImage,SvgImage,Image3D,ShowLayer,NewLayer,MergeLayers,AreaToMask,SmoothMask,FeatheredMask,SplitLongMask,ImageCropByAlpha,EnhanceImage,FaceToMask
from .nodes.ImageNode import SplitImage,GridOutput,GetImageSize_,MirroredImage,ImageColorTransfer,NoiseImage,TransparentImage,GradientImage,LoadImagesFromPath,LoadImagesFromURL,ResizeImage,TextImage,SvgImage,Image3D,ShowLayer,NewLayer,MergeLayers,AreaToMask,SmoothMask,SplitLongMask,ImageCropByAlpha,EnhanceImage,FaceToMask
from .nodes.Vae import VAELoader,VAEDecode
from .nodes.ScreenShareNode import ScreenShareNode,FloatingVideo
from .nodes.Clipseg import CLIPSeg,CombineMasks
from .nodes.ChatGPT import ChatGPTNode,ShowTextForGPT,CharacterInText
from .nodes.Audio import GamePal,SpeechRecognition,SpeechSynthesis
from .nodes.Utils import TESTNODE_,AppInfo,IntNumber,FloatSlider,TextInput,ColorInput,FontInput,TextToNumber,DynamicDelayProcessor,LimitNumber,SwitchByIndex,MultiplicationNode
from .nodes.Mask import OutlineMask
from .nodes.Mask import OutlineMask,FeatheredMask


# 要导出的所有节点及其名称的字典
Expand Down
73 changes: 0 additions & 73 deletions nodes/ImageNode.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,79 +847,6 @@ def run(self,mask,smoothness):



class FeatheredMask:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mask": ("MASK",),
"start_offset":("INT", {"default": 1,
"min": -150,
"max": 150,
"step": 1,
"display": "slider"}),
"feathering_weight":("FLOAT", {"default": 0.1,
"min": 0.0,
"max": 1,
"step": 0.1,
"display": "slider"})
}
}

RETURN_TYPES = ('MASK',)

FUNCTION = "run"

CATEGORY = "♾️Mixlab/Mask"

OUTPUT_IS_LIST = (False,)

# 运行的函数
def run(self,mask,start_offset, feathering_weight):
# print(mask.shape,mask.size())

image=tensor2pil(mask)

# Open the image using PIL
image = image.convert("L")
if start_offset>0:
image=ImageOps.invert(image)

# Convert the image to a numpy array
image_np = np.array(image)

# Use Canny edge detection to get black contours
edges = cv2.Canny(image_np, 30, 150)

for i in range(0,abs(start_offset)):
# int(100*feathering_weight)
a=int(abs(start_offset)*0.1*i)
# Dilate the black contours to make them wider
kernel = np.ones((a, a), np.uint8)

dilated_edges = cv2.dilate(edges, kernel, iterations=1)
# dilated_edges = cv2.erode(edges, kernel, iterations=1)
# Smooth the dilated edges using Gaussian blur
smoothed_edges = cv2.GaussianBlur(dilated_edges, (5, 5), 0)

# Adjust the feathering weight
feathering_weight = max(0, min(feathering_weight, 1))

# Blend the smoothed edges with the original image to achieve feathering effect
image_np = cv2.addWeighted(image_np, 1, smoothed_edges, feathering_weight, feathering_weight)

# Convert the result back to PIL image
result_image = Image.fromarray(np.uint8(image_np))
result_image=result_image.convert("L")

if start_offset>0:
result_image=ImageOps.invert(result_image)

mask=pil2tensor(result_image)
# print(mask.shape,mask.size())
return mask




class SplitLongMask:
Expand Down
106 changes: 103 additions & 3 deletions nodes/Mask.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import numpy as np
import scipy.ndimage
import torch
import comfy.utils

from nodes import MAX_RESOLUTION

import numpy as np
# from PIL import Image, ImageDraw
from PIL import Image, ImageOps

from comfy.cli_args import args
import cv2



# Tensor to PIL
def tensor2pil(image):
return Image.fromarray(np.clip(255. * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8))

# Convert PIL to Tensor
def pil2tensor(image):
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)


def grow(mask, expand, tapered_corners):
c = 0 if tapered_corners else 1
Expand Down Expand Up @@ -68,4 +84,88 @@ def run(self, mask, outline_width, tapered_corners):

m3=combine(m1,m2,0,0)

return (m3,)
return (m3,)




class FeatheredMask:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mask": ("MASK",),
"start_offset":("INT", {"default": 1,
"min": -150,
"max": 150,
"step": 1,
"display": "slider"}),
"feathering_weight":("FLOAT", {"default": 0.1,
"min": 0.0,
"max": 1,
"step": 0.1,
"display": "slider"})
}
}

RETURN_TYPES = ('MASK',)

FUNCTION = "run"

CATEGORY = "♾️Mixlab/Mask"

OUTPUT_IS_LIST = (True,)

# 运行的函数
def run(self,mask,start_offset, feathering_weight):
# print(mask.shape,mask.size())

num,_,_=mask.size()

masks=[]

for i in range(num):
mm=mask[i]
image=tensor2pil(mm)

# Open the image using PIL
image = image.convert("L")
if start_offset>0:
image=ImageOps.invert(image)

# Convert the image to a numpy array
image_np = np.array(image)

# Use Canny edge detection to get black contours
edges = cv2.Canny(image_np, 30, 150)

for i in range(0,abs(start_offset)):
# int(100*feathering_weight)
a=int(abs(start_offset)*0.1*i)
# Dilate the black contours to make them wider
kernel = np.ones((a, a), np.uint8)

dilated_edges = cv2.dilate(edges, kernel, iterations=1)
# dilated_edges = cv2.erode(edges, kernel, iterations=1)
# Smooth the dilated edges using Gaussian blur
smoothed_edges = cv2.GaussianBlur(dilated_edges, (5, 5), 0)

# Adjust the feathering weight
feathering_weight = max(0, min(feathering_weight, 1))

# Blend the smoothed edges with the original image to achieve feathering effect
image_np = cv2.addWeighted(image_np, 1, smoothed_edges, feathering_weight, feathering_weight)

# Convert the result back to PIL image
result_image = Image.fromarray(np.uint8(image_np))
result_image=result_image.convert("L")

if start_offset>0:
result_image=ImageOps.invert(result_image)

result_image=result_image.convert("L")
mt=pil2tensor(result_image)
masks.append(mt)

# print( mt.size())
return (masks,)
2 changes: 1 addition & 1 deletion web/javascript/checkVersion_mixlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { app } from '../../../scripts/app.js'
const repoOwner = 'shadowcz007' // 替换为仓库的所有者
const repoName = 'comfyui-mixlab-nodes' // 替换为仓库的名称

const version = 'v0.13.0'
const version = 'v0.14.0'

fetch(`https://api.github.com/repos/${repoOwner}/${repoName}/releases/latest`)
.then(response => response.json())
Expand Down

0 comments on commit 8128796

Please sign in to comment.