diff --git a/comfy_extras/nodes_remote_images.py b/comfy_extras/nodes_remote_images.py new file mode 100644 index 000000000..bbe43fb97 --- /dev/null +++ b/comfy_extras/nodes_remote_images.py @@ -0,0 +1,38 @@ +import torch +from urllib import request +from PIL import Image +from PIL.PngImagePlugin import PngInfo +import numpy as np + +class LoadImageUrl: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "url": ("STRING", { "multiline": False, }) + } + } + + RETURN_TYPES = ("IMAGE", "MASK") + FUNCTION = "load_image" + CATEGORY = "image" + + def load_image(self, url): + r = request.urlopen(url) + i = Image.open(r) + image = i.convert("RGB") + image = np.array(image).astype(np.float32) / 255.0 + image = torch.from_numpy(image)[None,] + if 'A' in i.getbands(): + mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0 + mask = 1. - torch.from_numpy(mask) + else: + mask = torch.zeros((64,64), dtype=torch.float32, device="cpu") + return (image, mask) + +NODE_CLASS_MAPPINGS = { + "LoadImageUrl": LoadImageUrl, +} diff --git a/nodes.py b/nodes.py index bf850e7d2..d37465d9c 100644 --- a/nodes.py +++ b/nodes.py @@ -10,7 +10,6 @@ import traceback from PIL import Image from PIL.PngImagePlugin import PngInfo import numpy as np -from urllib import request sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy")) @@ -937,35 +936,6 @@ class LoadImageMask: m.update(f.read()) return m.digest().hex() -class LoadImageUrl: - def __init__(self): - pass - - @classmethod - def INPUT_TYPES(s): - return { - "required": { - "url": ("STRING", { "multiline": False, }) - } - } - - RETURN_TYPES = ("IMAGE", "MASK") - FUNCTION = "load_image" - CATEGORY = "image" - - def load_image(self, url): - r = request.urlopen(url) - i = Image.open(r) - image = i.convert("RGB") - image = np.array(image).astype(np.float32) / 255.0 - image = torch.from_numpy(image)[None,] - if 'A' in i.getbands(): - mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0 - mask = 1. - torch.from_numpy(mask) - else: - mask = torch.zeros((64,64), dtype=torch.float32, device="cpu") - return (image, mask) - class ImageScale: upscale_methods = ["nearest-exact", "bilinear", "area"] crop_methods = ["disabled", "center"] @@ -1082,7 +1052,6 @@ NODE_CLASS_MAPPINGS = { "PreviewImage": PreviewImage, "LoadImage": LoadImage, "LoadImageMask": LoadImageMask, - "LoadImageUrl": LoadImageUrl, "ImageScale": ImageScale, "ImageInvert": ImageInvert, "ImagePadForOutpaint": ImagePadForOutpaint, @@ -1144,4 +1113,5 @@ def load_custom_nodes(): def init_custom_nodes(): load_custom_nodes() - load_custom_node(os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras"), "nodes_upscale_model.py")) \ No newline at end of file + load_custom_node(os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras"), "nodes_upscale_model.py")) + load_custom_node(os.path.join(os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy_extras"), "nodes_remote_images.py"))