add ImageOpacity node

This commit is contained in:
chflame163
2024-01-18 22:15:51 +08:00
committed by GitHub
parent d277de077b
commit ef11980aa1
3 changed files with 93 additions and 1 deletions
+14
View File
@@ -81,6 +81,20 @@ Node options:
Output type:
* image
### ImageOpacity:
Adjust image opacity
![image](image/image_opacity_example.png)
Node option:
* image: Image input, supporting RGB and RGBA. if is RGB, the alpha channel of the entire image will be automatically added.
* mask: Mask input. Attention that if the image input has alpha channel and different masks are input simultaneously, the original alpha channel will be overwritten by the mask.
* invert_mask: Whether to reverse the mask.
* opacity: Opacity of image.
Output type:
* image(RGBA)
* mask
## Example workflow
image Some JSON workflow files in the workflow directory, that is example for ComfyUI.
+16 -1
View File
@@ -3,7 +3,8 @@ ComfyUI
当前已完成Drop Shadow和OuterGlow节点,后续工作进行中。
## 更新说明:
* 现在layer_mask不是输入必选。不输入将使用layer_image的alpha通道。允许使用形状不同的layer和mask,但是尺寸必须一致。
* 添加ImageOpacity节点。
* layer_mask不是输入必选。不输入将使用layer_image的alpha通道。允许使用形状不同的layer和mask,但是尺寸必须一致。
![image](image/title.png)
* 添加ImageBlend节点
* DropShadow节点去掉调试mask通道输出;添加OuterGlow节点。
@@ -76,6 +77,20 @@ ComfyUI
输出:
* image
### ImageOpacity:
调整图像不透明度。
![image](image/image_opacity_example.png)
节点选项说明:
* image: 图像输入,支持RGB和RGBA输入。如果输入的是RGB,将自动添加整个图像的alpha通道。
* mask: 遮罩输入。注意如果image输入有alpha通道且同时输入了不同的mask,原本的alpha通道将被mask覆盖。
* invert_mask: 是否反转遮罩。
* opacity: 不透明度。
输出:
* image(RGBA)
* mask
## 使用示例:
在workflow目录下有json格式的工作流示例文件。
+63
View File
@@ -0,0 +1,63 @@
from .imagefunc import *
class ImageOpacity:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(self):
return {
"required": {
"image": ("IMAGE", ), #
"opacity": ("INT", {"default": 100, "min": 0, "max": 100, "step": 1}), # 透明度
"invert_mask": ("BOOLEAN", {"default": True}), # 反转mask
},
"optional": {
"mask": ("MASK",), #
}
}
RETURN_TYPES = ("IMAGE", "MASK",)
RETURN_NAMES = ("image", "mask",)
FUNCTION = 'image_opacity'
CATEGORY = '😺dzNodes'
OUTPUT_NODE = True
def image_opacity(self, image, opacity, invert_mask,
mask=None,
):
_image = tensor2pil(image).convert('RGB')
_mask = tensor2pil(image).convert('RGBA').split()[-1]
if mask is not None:
_mask = mask2image(mask).convert('L')
# 设置透明度
if invert_mask:
_color = Image.new("L", _image.size, color=(255))
else:
_color = Image.new("L", _image.size, color=(0))
# opacity
ret_mask = _mask
if opacity == 0:
ret_mask = _color
elif opacity < 100:
alpha = 1.0 - float(opacity) / 100
ret_mask = Image.blend(_mask, _color, alpha)
R, G, B, = _image.split()
if invert_mask:
ret_image = Image.merge('RGBA', (R, G, B, ImageChops.invert(ret_mask)))
else:
ret_image = Image.merge('RGBA', (R, G, B, ret_mask))
return (pil2tensor(ret_image), image2mask(ret_mask),)
NODE_CLASS_MAPPINGS = {
"LayerStyle_ImageOpacity": ImageOpacity
}
NODE_DISPLAY_NAME_MAPPINGS = {
"LayerStyle_ImageOpacity": "LayerStyle: ImageOpacity"
}