diff --git a/py/utils.py b/py/utils.py index 3f6d959..2485a9a 100644 --- a/py/utils.py +++ b/py/utils.py @@ -380,32 +380,22 @@ class LG_LoadImage(LoadImage): return LoadImage.IS_CHANGED(image) def load_image(self, image, keep_alpha=False): - image_path = folder_paths.get_annotated_filepath(image) + # 先调用父类方法获取完整的图像和遮罩 + image_tensor, mask_tensor = super().load_image(image) - i = Image.open(image_path) - i = ImageOps.exif_transpose(i) + if keep_alpha: + # 如果需要保持alpha,重新加载为RGBA格式 + image_path = folder_paths.get_annotated_filepath(image) + i = Image.open(image_path) + i = ImageOps.exif_transpose(i) + + if 'A' in i.getbands(): + image = i.convert("RGBA") + image = np.array(image).astype(np.float32) / 255.0 + image_tensor = torch.from_numpy(image)[None,] - if keep_alpha and 'A' in i.getbands(): - # 保持RGBA格式 - image = i.convert("RGBA") - image = np.array(image).astype(np.float32) / 255.0 - image = torch.from_numpy(image)[None,] - - # 提取alpha通道作为mask - alpha = image[:, :, :, 3] - mask = 1.0 - alpha # ComfyUI中mask的约定:1为遮罩,0为透明 - - return (image, mask.unsqueeze(0)) - else: - # 转换为RGB格式(原有逻辑) - image = i.convert("RGB") - image = np.array(image).astype(np.float32) / 255.0 - image = torch.from_numpy(image)[None,] - - # 创建空的mask - mask = torch.zeros((image.shape[1], image.shape[2]), dtype=torch.float32, device="cpu") - - return (image, mask.unsqueeze(0)) + # 遮罩直接使用父类提取的结果,不受keep_alpha影响 + return (image_tensor, mask_tensor) @PromptServer.instance.routes.get("/lg/get/latest_image") diff --git a/pyproject.toml b/pyproject.toml index 08aab2d..73f5697 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "comfyui_lg_tools" description = "This is a toolset designed for ComfyUI by LAOGOU-666, providing a series of practical image processing and operation nodes, making our operation more intuitive and convenient" -version = "1.0.3" +version = "1.0.4" license = { text = "MIT License" } dependencies = ["opencv-contrib-python"]