diff --git a/README.MD b/README.MD index 8d5c275..c3ab7df 100644 --- a/README.MD +++ b/README.MD @@ -80,7 +80,7 @@ When this error has occurred, please check the network environment. ## Update **If the dependency package error after updating, please reinstall the relevant dependency packages.
- +* [ImageRemoveAlpha](#ImageRemoveAlpha) node add optional mask input. * Commit [HLFrequencyDetailRestore](#HLFrequencyDetailRestore) node, Using low-frequency filtering and high-frequency preserving to restore image details, the fusion is better. * Commit [AddGrain](#AddGrain) and [MaskGrain](#MaskGrain) nodes, Add noise to a picture or mask. * Commit [FilmV2](#FilmV2) node, The fastgrain method is added on the basis of the previous one, and the noise generation speed is 10 times faster. @@ -1002,6 +1002,8 @@ Remove the alpha channel from the image and convert it to RGB mode. you can choo Node options: ![image](image/image_remove_alpha_node.jpg) +* RGBA_image: The input image supports RGBA or RGB modes. +* mask: Optional input mask. If there is an input mask, it will be used first, ignoring the alpha that comes with RGBA_image. * fill_background: Whether to fill the background. * background_color4: Color of background. diff --git a/README_CN.MD b/README_CN.MD index f733dfa..fbf875f 100644 --- a/README_CN.MD +++ b/README_CN.MD @@ -80,6 +80,7 @@ git clone https://github.com/chflame163/ComfyUI_LayerStyle.git ## 更新说明 **如果本插件更新后出现依赖包错误,请重新安装相关依赖包。 +* [ImageRemoveAlpha](#ImageRemoveAlpha) 节点增加mask可选输入。 * 添加 [HLFrequencyDetailRestore](#HLFrequencyDetailRestore)节点, 使用低频滤波加保留高频来恢复图像细节,图像融合性更好。 * 添加 [AddGrain](#AddGrain) 和 [MaskGrain](#MaskGrain) 节点, 为图片或遮罩添加噪声。 * 添加 [FilmV2](#FilmV2) 节点, 在之前基础上增加了fastgrain方法,生成噪点速度加快了10倍。 @@ -990,6 +991,8 @@ GradientImage的V2升级版。 节点选项说明: ![image](image/image_remove_alpha_node.jpg) +* RGBA_image: 输入的图像,支持RGBA或RGB模式。 +* mask:可选输入遮罩。如果有输入遮罩将优先使用, 忽略RGBA_image自带的alpha。 * fill_background: 是否填充背景。 * background_color4: 背景颜色。 diff --git a/image/image_remove_alpha_node.jpg b/image/image_remove_alpha_node.jpg index c43d624..95b6c25 100644 Binary files a/image/image_remove_alpha_node.jpg and b/image/image_remove_alpha_node.jpg differ diff --git a/py/image_remove_alpha.py b/py/image_remove_alpha.py index f96f94f..7665ac0 100644 --- a/py/image_remove_alpha.py +++ b/py/image_remove_alpha.py @@ -17,6 +17,7 @@ class ImageRemoveAlpha: "background_color": ("STRING", {"default": "#000000"}), }, "optional": { + "mask": ("MASK",), # } } @@ -25,21 +26,27 @@ class ImageRemoveAlpha: FUNCTION = 'image_remove_alpha' CATEGORY = '😺dzNodes/LayerUtility' - def image_remove_alpha(self, RGBA_image, fill_background, background_color): + def image_remove_alpha(self, RGBA_image, fill_background, background_color, mask=None): ret_images = [] - for i in RGBA_image: - i = torch.unsqueeze(i, 0) - _image = tensor2pil(i) - if _image.mode != "RGBA": - log(f"Error: {NODE_NAME} skipped, because the input image is not RGBA.", message_type='error') - return (RGBA_image) + for index, img in enumerate(RGBA_image): + _image = tensor2pil(img) + if fill_background: - alpha = _image.split()[-1] + if mask is not None: + m = mask[index].unsqueeze(0) if index < len(mask) else mask[-1].unsqueeze(0) + alpha = tensor2pil(m).convert('L') + elif _image.mode == "RGBA": + alpha = _image.split()[-1] + else: + log(f"Error: {NODE_NAME} skipped, because the input image is not RGBA and mask is None.", + message_type='error') + return (RGBA_image,) ret_image = Image.new('RGB', size=_image.size, color=background_color) ret_image.paste(_image, mask=alpha) ret_images.append(pil2tensor(ret_image)) + else: ret_images.append(pil2tensor(tensor2pil(i).convert('RGB')))