Upload salia_get_img.py
Browse files- salia_get_img.py +125 -0
salia_get_img.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
from urllib.parse import quote, urlsplit, urlunsplit
|
| 3 |
+
from urllib.request import Request, urlopen
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
import torch
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
BASE_URL = "https://huggingface.co/saliacoel/x/resolve/main/"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def _strip_outer_quotes(s: str) -> str:
|
| 14 |
+
s = s.strip()
|
| 15 |
+
if len(s) >= 2 and ((s[0] == '"' and s[-1] == '"') or (s[0] == "'" and s[-1] == "'")):
|
| 16 |
+
s = s[1:-1].strip()
|
| 17 |
+
return s
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def _last_path_part(path: str) -> str:
|
| 21 |
+
return path.rsplit("/", 1)[-1]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _has_extension(path: str) -> bool:
|
| 25 |
+
tail = _last_path_part(path)
|
| 26 |
+
return "." in tail and not tail.endswith(".")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def _append_png_if_missing_to_url(url: str) -> str:
|
| 30 |
+
parts = urlsplit(url)
|
| 31 |
+
path = parts.path
|
| 32 |
+
if not _has_extension(path):
|
| 33 |
+
path = path + ".png"
|
| 34 |
+
return urlunsplit((parts.scheme, parts.netloc, path, parts.query, parts.fragment))
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _make_url(value: str) -> str:
|
| 38 |
+
value = _strip_outer_quotes(value)
|
| 39 |
+
if not value:
|
| 40 |
+
raise ValueError("salia_get_img: input string is empty.")
|
| 41 |
+
|
| 42 |
+
lower = value.lower()
|
| 43 |
+
if lower.startswith("http://") or lower.startswith("https://"):
|
| 44 |
+
return _append_png_if_missing_to_url(value)
|
| 45 |
+
|
| 46 |
+
rel = value.lstrip("/")
|
| 47 |
+
if not _has_extension(rel):
|
| 48 |
+
rel += ".png"
|
| 49 |
+
|
| 50 |
+
# Quote each path segment, but keep slashes.
|
| 51 |
+
rel = "/".join(quote(part) for part in rel.split("/"))
|
| 52 |
+
return BASE_URL + rel
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def _download_bytes(url: str) -> bytes:
|
| 56 |
+
req = Request(
|
| 57 |
+
url,
|
| 58 |
+
headers={
|
| 59 |
+
"User-Agent": "Mozilla/5.0",
|
| 60 |
+
"Accept": "*/*",
|
| 61 |
+
},
|
| 62 |
+
)
|
| 63 |
+
with urlopen(req, timeout=60) as resp:
|
| 64 |
+
return resp.read()
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def _pil_to_comfy_image_tensor(img: Image.Image) -> torch.Tensor:
|
| 68 |
+
# Preserve alpha if present; otherwise use RGB.
|
| 69 |
+
has_alpha = ("A" in img.getbands()) or ("transparency" in img.info)
|
| 70 |
+
|
| 71 |
+
if has_alpha:
|
| 72 |
+
img = img.convert("RGBA")
|
| 73 |
+
else:
|
| 74 |
+
img = img.convert("RGB")
|
| 75 |
+
|
| 76 |
+
arr = np.array(img, dtype=np.float32) / 255.0 # HWC, 3 or 4 channels
|
| 77 |
+
tensor = torch.from_numpy(arr).unsqueeze(0) # BHWC
|
| 78 |
+
return tensor
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
class Salia_Get_Img:
|
| 82 |
+
@classmethod
|
| 83 |
+
def INPUT_TYPES(cls):
|
| 84 |
+
return {
|
| 85 |
+
"required": {
|
| 86 |
+
"img_name_or_url": (
|
| 87 |
+
"STRING",
|
| 88 |
+
{
|
| 89 |
+
"default": "FeatherMask",
|
| 90 |
+
"multiline": False,
|
| 91 |
+
},
|
| 92 |
+
),
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
RETURN_TYPES = ("IMAGE",)
|
| 97 |
+
RETURN_NAMES = ("img",)
|
| 98 |
+
FUNCTION = "get_img"
|
| 99 |
+
CATEGORY = "Salia"
|
| 100 |
+
|
| 101 |
+
def get_img(self, img_name_or_url):
|
| 102 |
+
url = _make_url(img_name_or_url)
|
| 103 |
+
|
| 104 |
+
try:
|
| 105 |
+
data = _download_bytes(url)
|
| 106 |
+
except Exception as e:
|
| 107 |
+
raise ValueError(f"salia_get_img: failed to download image from URL:\n{url}\n\n{e}") from e
|
| 108 |
+
|
| 109 |
+
try:
|
| 110 |
+
img = Image.open(io.BytesIO(data))
|
| 111 |
+
img.load()
|
| 112 |
+
except Exception as e:
|
| 113 |
+
raise ValueError(f"salia_get_img: downloaded data is not a valid image:\n{url}\n\n{e}") from e
|
| 114 |
+
|
| 115 |
+
image_tensor = _pil_to_comfy_image_tensor(img)
|
| 116 |
+
return (image_tensor,)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
NODE_CLASS_MAPPINGS = {
|
| 120 |
+
"salia_get_img": Salia_Get_Img,
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
NODE_DISPLAY_NAME_MAPPINGS = {
|
| 124 |
+
"salia_get_img": "salia_get_img",
|
| 125 |
+
}
|