-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.py
More file actions
65 lines (48 loc) · 2.03 KB
/
Copy pathnodes.py
File metadata and controls
65 lines (48 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import hashlib
import io
import urllib.request
import urllib.error
import numpy as np
import torch
from PIL import Image, ImageOps
class LoadImageFromHttpURLNorm:
"""Loads an image from an HTTP/HTTPS URL with automatic EXIF orientation correction."""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image_url": ("STRING", {"default": "", "multiline": False}),
},
}
RETURN_TYPES = ("IMAGE", "MASK", "INT", "INT")
RETURN_NAMES = ("image", "mask", "width", "height")
FUNCTION = "load_image"
CATEGORY = "image"
def load_image(self, image_url: str):
if not image_url or not image_url.strip():
raise ValueError("image_url is empty")
image_url = image_url.strip()
req = urllib.request.Request(image_url, headers={"User-Agent": "ComfyUI"})
with urllib.request.urlopen(req, timeout=30) as resp:
data = resp.read()
img = Image.open(io.BytesIO(data))
# Apply EXIF orientation — handles all 8 orientation tag values
# (rotation, mirroring) and strips the tag so downstream code
# doesn't double-rotate.
img = ImageOps.exif_transpose(img)
has_alpha = img.mode == "RGBA" or "A" in img.getbands()
img = img.convert("RGBA") if has_alpha else img.convert("RGB")
# IMAGE tensor: (1, H, W, C) float32 in [0, 1]
image_np = np.array(img).astype(np.float32) / 255.0
if has_alpha:
rgb = image_np[:, :, :3]
alpha = image_np[:, :, 3]
else:
rgb = image_np
alpha = np.ones((image_np.shape[0], image_np.shape[1]), dtype=np.float32)
image_tensor = torch.from_numpy(rgb).unsqueeze(0) # (1, H, W, 3)
mask_tensor = 1.0 - torch.from_numpy(alpha).unsqueeze(0) # (1, H, W)
return (image_tensor, mask_tensor, image_tensor.shape[2], image_tensor.shape[1])
@classmethod
def IS_CHANGED(cls, image_url: str):
return hashlib.sha256(image_url.strip().encode()).hexdigest()