WASasquatch / was-node-suite-comfyui

An extensive node suite for ComfyUI with over 210 new nodes
MIT License
1.23k stars 178 forks source link

Load Image Batch can't output mask #259

Closed akafun closed 1 year ago

akafun commented 1 year ago

Load Image Batch can't output mask,Output mask node, useful in processing many png images.

At line 5071 of the source code

def load_batch_images(self, path, pattern='*', index=0, mode="single_image", label='Batch 001', allow_RGBA_output='false', filename_text_extension='true'):
        allow_RGBA_output = (allow_RGBA_output == 'true')
        if not os.path.exists(path):
            return (None, )
        fl = self.BatchImageLoader(path, label, pattern)
        new_paths = fl.image_paths
        if mode == 'single_image':
            image, filename = fl.get_image_by_id(index)
            if image == None:
                cstr(f"No valid image was found for the inded `{index}`").error.print()
                return (None, None)
        else:
            image, filename = fl.get_next_image()
            if image == None:
                cstr(f"No valid image was found for the next ID. Did you remove images from the source directory?").error.print()
                return (None, None)
        # Update history
        update_history_images(new_paths)
        if not allow_RGBA_output:
           image = image.convert("RGB")
        #addcodeSart
        if 'A' in image.getbands():
            mask = np.array(image.getchannel('A')).astype(np.float32) / 255.0
            mask = 1. - torch.from_numpy(mask)
        else:
            mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu")   
        #addcodeEnd   
        if filename_text_extension == "false":
            filename = os.path.splitext(filename)[0]
        return (pil2tensor(image),mask,filename)

Below is the relevant code, if I insert this code(#addcodeSart), will it become useful? Code from single image load Then return this mask and make sure RETURN_NAMES = ("image","mask","filename_text")

WASasquatch commented 1 year ago

That won't work if RGBA is not allowed, and should come before converting to RGB, that way an alpha channel can be scraped before it's truncated. Additionally masks are not batched aren't they? So maybe need a .unsqueeze(0)

akafun commented 1 year ago

That won't work if RGBA is not allowed, and should come before converting to RGB, that way an alpha channel can be scraped before it's truncated. Additionally masks are not batched aren't they? So maybe need a .unsqueeze(0)

Thanks, it worked