From 1142651d69465af4525a757c69014bef89671f19 Mon Sep 17 00:00:00 2001 From: Yolan Date: Fri, 28 Jul 2023 19:28:01 -0700 Subject: [PATCH] Fix image url loader --- __init__.py | 26 +++++++++++++++++++++++--- requirements.txt | 1 + 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 requirements.txt diff --git a/__init__.py b/__init__.py index a3dde7e..637d7bf 100644 --- a/__init__.py +++ b/__init__.py @@ -1,3 +1,6 @@ +from io import BytesIO + +import requests as requests import torch import os @@ -17,6 +20,7 @@ import comfy.model_management import comfy from PIL import Image, ImageOps +import requests import folder_paths from comfy_extras.chainner_models import model_loading @@ -263,6 +267,23 @@ class DTLoadLatent: return "Invalid latent file: {}".format(latent) return True +def load_image_from_url(url): + try: + # Send a GET request to fetch the image data + response = requests.get(url) + + # Check if the request was successful + response.raise_for_status() + + # Read the image data and create a PIL image + image = Image.open(BytesIO(response.content)) + + return image + + except requests.exceptions.RequestException as e: + print(f"Error loading image from URL: {url}") + print(e) + return None class DTLoadImage: loaded_path = None @@ -281,7 +302,7 @@ class DTLoadImage: RETURN_TYPES = ("IMAGE", "MASK") FUNCTION = "load_image" def load_image(self, image): - i = Image.open(image) + i = load_image_from_url(image) i = ImageOps.exif_transpose(i) image = i.convert("RGB") image = np.array(image).astype(np.float32) / 255.0 @@ -325,8 +346,7 @@ class DTLoadImageMask: RETURN_TYPES = ("MASK",) FUNCTION = "load_image" def load_image(self, image, channel): - # Load the image from a url - i = Image.open(image) + i = load_image_from_url(image) i = ImageOps.exif_transpose(i) if i.getbands() != ("R", "G", "B", "A"): i = i.convert("RGBA") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..663bd1f --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file