Skip to content

Commit d740251

Browse files
authored
Improve image download method with redirect handling
Refactor image downloading logic to handle redirects and ensure image loading.
1 parent a1ca256 commit d740251

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

py/AILab_ImageMaskTools.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ def get_folder_list(cls):
547547
input_dir = folder_paths.get_input_directory()
548548
os.makedirs(input_dir, exist_ok=True)
549549
return [f for f in os.listdir(input_dir) if os.path.isdir(os.path.join(input_dir, f))]
550-
550+
551551
def download_image(self, url):
552552
try:
553553
import requests
@@ -557,14 +557,17 @@ def download_image(self, url):
557557
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
558558
}
559559

560-
response = requests.get(url, stream=True, timeout=10, headers=headers)
560+
response = requests.get(url, timeout=10, headers=headers, allow_redirects=True)
561561
if response.status_code != 200:
562562
raise ValueError(f"Failed to download image from URL: {url}, status code: {response.status_code}")
563-
return Image.open(BytesIO(response.content))
563+
564+
img = Image.open(BytesIO(response.content))
565+
img.load()
566+
return img
564567

565568
except Exception as e:
566569
print(f"Error downloading image from URL: {str(e)}")
567-
raise e
570+
raise
568571

569572
def get_image(self, image_path_or_URL="", image=""):
570573
if not image_path_or_URL and (not image or image == ""):

0 commit comments

Comments
 (0)