From 1a172ec7ae8b2075381344b9172add753966890e Mon Sep 17 00:00:00 2001 From: Cyber Dick Lang <286878701@qq.com> Date: Fri, 25 Jul 2025 17:23:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BImage=20Remove=20Alpha?= =?UTF-8?q?=E8=8A=82=E7=82=B9=EF=BC=9A=E5=B0=86background=5Fcolor=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E6=94=B9=E4=B8=BA=E9=A2=84=E8=AE=BE=E9=A2=9C=E8=89=B2?= =?UTF-8?q?=E4=B8=8B=E6=8B=89=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nodes/image/image_remove_alpha.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/nodes/image/image_remove_alpha.py b/nodes/image/image_remove_alpha.py index 604e4b3..36e0567 100644 --- a/nodes/image/image_remove_alpha.py +++ b/nodes/image/image_remove_alpha.py @@ -35,7 +35,7 @@ class ImageRemoveAlpha_UTK: "required": { "RGBA_image": ("IMAGE",), # "fill_background": ("BOOLEAN", {"default": False}), - "background_color": ("STRING", {"default": "#000000"}), + "background_color": (["black", "white", "gray", "red", "green", "blue", "yellow", "cyan", "magenta", "transparent"], {"default": "black"}), }, "optional": { "mask": ("MASK",), # @@ -49,6 +49,22 @@ class ImageRemoveAlpha_UTK: def image_remove_alpha( self, RGBA_image, fill_background, background_color, mask=None ): + # 颜色名称到颜色值的映射 + color_map = { + "black": "#000000", + "white": "#FFFFFF", + "gray": "#808080", + "red": "#FF0000", + "green": "#00FF00", + "blue": "#0000FF", + "yellow": "#FFFF00", + "cyan": "#00FFFF", + "magenta": "#FF00FF", + "transparent": None + } + + # 获取对应的颜色值 + bg_color = color_map.get(background_color, "#000000") ret_images = [] @@ -71,8 +87,15 @@ class ImageRemoveAlpha_UTK: message_type="error", ) return (RGBA_image,) - ret_image = Image.new("RGB", size=_image.size, color=background_color) - ret_image.paste(_image, mask=alpha) + + # 处理透明背景 + if bg_color is None: + # 如果选择透明,直接转换为RGB(透明部分变为白色) + ret_image = _image.convert("RGB") + else: + ret_image = Image.new("RGB", size=_image.size, color=bg_color) + ret_image.paste(_image, mask=alpha) + ret_images.append(pil2tensor(ret_image)) else: