diff --git a/Examples/NodeImagePre.json b/Examples/NodeImagePre.json index f747443..6d8cbf9 100644 --- a/Examples/NodeImagePre.json +++ b/Examples/NodeImagePre.json @@ -1 +1 @@ -{"last_node_id":27,"last_link_id":29,"nodes":[{"id":14,"type":"easy showAnything","pos":[1550,240],"size":[210,88],"flags":{},"order":12,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":14}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["800"]},{"id":13,"type":"easy showAnything","pos":[1550,150],"size":[210,88],"flags":{},"order":11,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":13}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["800"]},{"id":25,"type":"MaskPreview+","pos":[1270,-130],"size":[210,246],"flags":{},"order":3,"mode":0,"inputs":[{"name":"mask","localized_name":"mask","label":"mask","type":"MASK","link":27}],"outputs":[],"properties":{"cnr_id":"comfyui_essentials","ver":"33ff89fd354d8ec3ab6affb605a79a931b445d99","Node name for S&R":"MaskPreview+"},"widgets_values":[]},{"id":11,"type":"PreviewImage","pos":[1030,-130],"size":[210,246],"flags":{},"order":2,"mode":0,"inputs":[{"name":"images","localized_name":"图像","label":"images","type":"IMAGE","link":11}],"outputs":[],"properties":{"cnr_id":"comfy-core","ver":"0.3.26","Node name for S&R":"PreviewImage"},"widgets_values":[]},{"id":23,"type":"LoadImage","pos":[440,190],"size":[315,314],"flags":{},"order":0,"mode":0,"inputs":[],"outputs":[{"name":"IMAGE","localized_name":"图像","label":"IMAGE","type":"IMAGE","links":[24]},{"name":"MASK","localized_name":"遮罩","label":"MASK","type":"MASK","links":[25]}],"properties":{"cnr_id":"comfy-core","ver":"0.3.26","Node name for S&R":"LoadImage"},"widgets_values":["clipspace/clipspace-mask-37688.5.png [input]","image"]},{"id":18,"type":"DF_Get_latent_size","pos":[1270,160],"size":[210,78],"flags":{},"order":4,"mode":0,"inputs":[{"name":"latent","localized_name":"latent","label":"latent","type":"LATENT","link":18}],"outputs":[{"name":"WIDTH","localized_name":"WIDTH","label":"WIDTH","type":"INT","links":[13],"slot_index":0},{"name":"HEIGHT","localized_name":"HEIGHT","label":"HEIGHT","type":"INT","links":[14],"slot_index":1}],"properties":{"cnr_id":"derfuu_comfyui_moddednodes","ver":"d0905bed31249f2bd0814c67585cf4fe3c77c015","Node name for S&R":"DF_Get_latent_size"},"widgets_values":[false]},{"id":10,"type":"easy showAnything","pos":[1270,760],"size":[210,88],"flags":{},"order":9,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":10}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["1200"]},{"id":19,"type":"easy showAnything","pos":[1270,640],"size":[210,88],"flags":{},"order":8,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":19}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["1.5"]},{"id":16,"type":"easy showAnything","pos":[1270,520],"size":[210,88],"flags":{},"order":7,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":16}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["800"]},{"id":17,"type":"easy showAnything","pos":[1270,400],"size":[210,88],"flags":{},"order":6,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":17}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["800"]},{"id":15,"type":"easy showAnything","pos":[1270,280],"size":[210,88],"flags":{},"order":5,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":15}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["1"]},{"id":27,"type":"easy showAnything","pos":[1270,880],"size":[210,88],"flags":{},"order":10,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":29}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["1200"]},{"id":20,"type":"NodeImagePre","pos":[784.8815307617188,195.95396423339844],"size":[210,266],"flags":{},"order":1,"mode":0,"inputs":[{"name":"image","localized_name":"image","label":"image","type":"IMAGE","link":24},{"name":"mask","localized_name":"mask","label":"mask","type":"MASK","shape":7,"link":25}],"outputs":[{"name":"image_resize","localized_name":"image_resize","label":"image_resize","type":"IMAGE","links":[11],"slot_index":0},{"name":"mask_resize","localized_name":"mask_resize","label":"mask_resize","type":"MASK","links":[27],"slot_index":1},{"name":"empty_latent","localized_name":"empty_latent","label":"empty_latent","type":"LATENT","links":[18],"slot_index":2},{"name":"batch","localized_name":"batch","label":"batch","type":"INT","links":[15],"slot_index":3},{"name":"width_resize","localized_name":"width_resize","label":"width_resize","type":"INT","links":[17],"slot_index":4},{"name":"height_resize","localized_name":"height_resize","label":"height_resize","type":"INT","links":[16],"slot_index":5},{"name":"ups","localized_name":"ups","label":"ups","type":"FLOAT","links":[19]},{"name":"width_ups","localized_name":"width_ups","label":"width_ups","type":"INT","links":[10],"slot_index":7},{"name":"height_ups","localized_name":"height_ups","label":"height_ups","type":"INT","links":[29],"slot_index":8}],"properties":{"aux_id":"KERRY-YUAN/ComfyUI_Simple_Executor","ver":"6ccb3b46e1f93bf176a612fb86e34882ba3200f5","Node name for S&R":"NodeImagePre"},"widgets_values":[800,1,1.5]}],"links":[[10,20,7,10,0,"*"],[11,20,0,11,0,"IMAGE"],[13,18,0,13,0,"*"],[14,18,1,14,0,"*"],[15,20,3,15,0,"*"],[16,20,5,16,0,"*"],[17,20,4,17,0,"*"],[18,20,2,18,0,"LATENT"],[19,20,6,19,0,"*"],[24,23,0,20,0,"IMAGE"],[25,23,1,20,1,"MASK"],[27,20,1,25,0,"MASK"],[29,20,8,27,0,"*"]],"groups":[],"config":{},"extra":{"ds":{"scale":0.7247295000000004,"offset":[204.0870467778224,215.44951852340856]},"ue_links":[]},"version":0.4} \ No newline at end of file +{"last_node_id":27,"last_link_id":29,"nodes":[{"id":14,"type":"easy showAnything","pos":[1550,240],"size":[210,88],"flags":{},"order":12,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":14}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["800"]},{"id":13,"type":"easy showAnything","pos":[1550,150],"size":[210,88],"flags":{},"order":11,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":13}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["800"]},{"id":25,"type":"MaskPreview+","pos":[1270,-130],"size":[210,246],"flags":{},"order":3,"mode":0,"inputs":[{"name":"mask","localized_name":"mask","label":"mask","type":"MASK","link":27}],"outputs":[],"properties":{"cnr_id":"comfyui_essentials","ver":"33ff89fd354d8ec3ab6affb605a79a931b445d99","Node name for S&R":"MaskPreview+"},"widgets_values":[]},{"id":11,"type":"PreviewImage","pos":[1030,-130],"size":[210,246],"flags":{},"order":2,"mode":0,"inputs":[{"name":"images","localized_name":"图像","label":"images","type":"IMAGE","link":11}],"outputs":[],"properties":{"cnr_id":"comfy-core","ver":"0.3.26","Node name for S&R":"PreviewImage"},"widgets_values":[]},{"id":23,"type":"LoadImage","pos":[440,190],"size":[315,314],"flags":{},"order":0,"mode":0,"inputs":[],"outputs":[{"name":"IMAGE","localized_name":"图像","label":"IMAGE","type":"IMAGE","links":[24]},{"name":"MASK","localized_name":"遮罩","label":"MASK","type":"MASK","links":[25]}],"properties":{"cnr_id":"comfy-core","ver":"0.3.26","Node name for S&R":"LoadImage"},"widgets_values":["clipspace/clipspace-mask-37688.5.png [input]","image"]},{"id":18,"type":"DF_Get_latent_size","pos":[1270,160],"size":[210,78],"flags":{},"order":4,"mode":0,"inputs":[{"name":"latent","localized_name":"latent","label":"latent","type":"LATENT","link":18}],"outputs":[{"name":"WIDTH","localized_name":"WIDTH","label":"WIDTH","type":"INT","links":[13],"slot_index":0},{"name":"HEIGHT","localized_name":"HEIGHT","label":"HEIGHT","type":"INT","links":[14],"slot_index":1}],"properties":{"cnr_id":"derfuu_comfyui_moddednodes","ver":"d0905bed31249f2bd0814c67585cf4fe3c77c015","Node name for S&R":"DF_Get_latent_size"},"widgets_values":[false]},{"id":10,"type":"easy showAnything","pos":[1270,760],"size":[210,88],"flags":{},"order":9,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":10}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["1200"]},{"id":19,"type":"easy showAnything","pos":[1270,640],"size":[210,88],"flags":{},"order":8,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":19}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["1.5"]},{"id":16,"type":"easy showAnything","pos":[1270,520],"size":[210,88],"flags":{},"order":7,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":16}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["800"]},{"id":17,"type":"easy showAnything","pos":[1270,400],"size":[210,88],"flags":{},"order":6,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":17}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["800"]},{"id":15,"type":"easy showAnything","pos":[1270,280],"size":[210,88],"flags":{},"order":5,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":15}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["1"]},{"id":27,"type":"easy showAnything","pos":[1270,880],"size":[210,88],"flags":{},"order":10,"mode":0,"inputs":[{"name":"anything","localized_name":"输入任何","label":"anything","type":"*","shape":7,"link":29}],"outputs":[{"name":"output","localized_name":"output","label":"output","type":"*","links":null}],"properties":{"cnr_id":"comfyui-easy-use","ver":"45d5c08bbb354bcc7427a7774140e5aa4a2989cb","Node name for S&R":"easy showAnything"},"widgets_values":["1200"]},{"id":20,"type":"NodeImagePre","pos":[784.8815307617188,195.95396423339844],"size":[210,266],"flags":{},"order":1,"mode":0,"inputs":[{"name":"image","localized_name":"image","label":"image","type":"IMAGE","link":24},{"name":"mask","localized_name":"mask","label":"mask","type":"MASK","shape":7,"link":25}],"outputs":[{"name":"image_resize","localized_name":"image_resize","label":"image_resize","type":"IMAGE","links":[11],"slot_index":0},{"name":"mask_resize","localized_name":"mask_resize","label":"mask_resize","type":"MASK","links":[27],"slot_index":1},{"name":"empty_latent","localized_name":"empty_latent","label":"empty_latent","type":"LATENT","links":[18],"slot_index":2},{"name":"batch","localized_name":"batch","label":"batch","type":"INT","links":[15],"slot_index":3},{"name":"width_resize","localized_name":"width_resize","label":"width_resize","type":"INT","links":[17],"slot_index":4},{"name":"height_resize","localized_name":"height_resize","label":"height_resize","type":"INT","links":[16],"slot_index":5},{"name":"ups","localized_name":"ups","label":"ups","type":"FLOAT","links":[19]},{"name":"width_ups","localized_name":"width_ups","label":"width_ups","type":"INT","links":[10],"slot_index":7},{"name":"height_ups","localized_name":"height_ups","label":"height_ups","type":"INT","links":[29],"slot_index":8}],"properties":{"cnr_id":"simple_executor","ver":"6ccb3b46e1f93bf176a612fb86e34882ba3200f5","Node name for S&R":"NodeImagePre","aux_id":"KERRY-YUAN/ComfyUI_Simple_Executor"},"widgets_values":[800,1,1.5]}],"links":[[10,20,7,10,0,"*"],[11,20,0,11,0,"IMAGE"],[13,18,0,13,0,"*"],[14,18,1,14,0,"*"],[15,20,3,15,0,"*"],[16,20,5,16,0,"*"],[17,20,4,17,0,"*"],[18,20,2,18,0,"LATENT"],[19,20,6,19,0,"*"],[24,23,0,20,0,"IMAGE"],[25,23,1,20,1,"MASK"],[27,20,1,25,0,"MASK"],[29,20,8,27,0,"*"]],"groups":[],"config":{},"extra":{"ds":{"scale":0.45,"offset":[1145.6740247938367,651.0460357666016]},"ue_links":[]},"version":0.4} \ No newline at end of file diff --git a/Examples/NodeImagePre.png b/Examples/NodeImagePre.png index 3a5fc12..06d54d1 100644 Binary files a/Examples/NodeImagePre.png and b/Examples/NodeImagePre.png differ diff --git a/NodeSimpleExecutor.py b/NodeSimpleExecutor.py index f884440..98bc669 100644 --- a/NodeSimpleExecutor.py +++ b/NodeSimpleExecutor.py @@ -139,34 +139,55 @@ class NodeImagePre: new_h = max(divisor, ((round(h * scale) + divisor - 1) // divisor) * divisor) return new_w, new_h - def _resize_tensor(self, tensor, size): - if tensor is None: - return None - img_np = tensor[0].cpu().numpy() - img_np = np.clip(img_np * 255. if np.max(img_np) <= 1.0 else img_np, 0, 255).astype(np.uint8) - pil_mode = 'RGB' if img_np.ndim == 3 and img_np.shape[2] >= 3 else 'L' - img_np_proc = img_np[:, :, :3] if pil_mode == 'RGB' else img_np - img = Image.fromarray(img_np_proc, pil_mode) - img = img.resize(size, Image.Resampling.LANCZOS) if img.size != size else img - img_out_np = np.array(img).astype(np.float32) / 255.0 - img_out_np = img_out_np[..., np.newaxis] if img_out_np.ndim == 2 else img_out_np - return torch.from_numpy(img_out_np).unsqueeze(0) + def _tensor2pil(self, tensor): + if tensor.dim() == 4: + tensor = tensor.squeeze(0) + return Image.fromarray(np.clip(tensor.numpy().squeeze()*255, 0, 255).astype(np.uint8)) + + def _pil2tensor(self, image): + return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0) + + def _resize_image(self, tensor, size): + image = self._tensor2pil(tensor) + return self._pil2tensor(image.resize(size, Image.Resampling.LANCZOS)) + + def _resize_mask(self, tensor, size): + mask = self._tensor2pil(tensor).convert("L") + resized = mask.resize(size, Image.Resampling.NEAREST) + return torch.from_numpy(np.array(resized)).float().unsqueeze(0) def execute(self, image, shortside, batch, ups, mask=None): _, h, w, _ = image.shape resize_w, resize_h = self._process_size(w, h, shortside, 16) - image_out = self._resize_tensor(image, (resize_w, resize_h)) - mask_out_resized = self._resize_tensor(mask, (resize_w, resize_h)) - latent = {"samples": torch.zeros((batch, 4, max(1, resize_h // 8), max(1, resize_w // 8)))} - ups_w, ups_h = self._process_size(resize_w, resize_h, max(8, int(min(resize_w, resize_h) * ups)), 8) - return (image_out, mask_out_resized, latent, batch, + + image_out = self._resize_image(image, (resize_w, resize_h)) + + mask_out_resized = None + if mask is not None: + if mask.dim() == 2: + mask = mask.unsqueeze(0).unsqueeze(-1) + elif mask.dim() == 3: + mask = mask.unsqueeze(-1) + mask_out_resized = self._resize_mask(mask, (resize_w, resize_h)) + + latent = {"samples": torch.zeros(( + batch, + 4, + max(1, resize_h // 8), + max(1, resize_w // 8) + ))} + + ups_w, ups_h = self._process_size(resize_w, resize_h, + max(8, int(min(resize_w, resize_h) * ups)), 8) + + return (image_out, mask_out_resized, latent, batch, resize_w, resize_h, ups, ups_w, ups_h) #---------------------------------------------------------------------------------------------------------------------------------------------------# #Node Registration / 节点注册 NODE_CLASS_MAPPINGS = { + "NodeAutoSampler": NodeAutoSampler, "NodeImageResize": NodeImageResize, "NodeImagePre": NodeImagePre, - "NodeAutoSampler": NodeAutoSampler, } \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 5cd3f3c..cde04ad 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "simple_executor" description = "This node package contains automatic sampler setting according to model name in ComfyUI, adjusting image size according to specific constraints and some other nodes." -version = "0.1.0" +version = "0.1.1" license = {file = "LICENSE"} dependencies = ["torch>=1.12.0", "numpy>=1.21.0", "Pillow>=8.0.0"]