Spaces:
Running
on
L40S
Running
on
L40S
import torch | |
import numpy as np | |
import scipy | |
import os | |
#import re | |
from pathlib import Path | |
import folder_paths | |
FONTS_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "fonts") | |
SCRIPT_DIR = Path(__file__).parent | |
folder_paths.add_model_folder_path("luts", (SCRIPT_DIR / "luts").as_posix()) | |
folder_paths.add_model_folder_path( | |
"luts", (Path(folder_paths.models_dir) / "luts").as_posix() | |
) | |
# from https://github.com/pythongosssss/ComfyUI-Custom-Scripts | |
class AnyType(str): | |
def __ne__(self, __value: object) -> bool: | |
return False | |
def min_(tensor_list): | |
# return the element-wise min of the tensor list. | |
x = torch.stack(tensor_list) | |
mn = x.min(axis=0)[0] | |
return torch.clamp(mn, min=0) | |
def max_(tensor_list): | |
# return the element-wise max of the tensor list. | |
x = torch.stack(tensor_list) | |
mx = x.max(axis=0)[0] | |
return torch.clamp(mx, max=1) | |
def expand_mask(mask, expand, tapered_corners): | |
c = 0 if tapered_corners else 1 | |
kernel = np.array([[c, 1, c], | |
[1, 1, 1], | |
[c, 1, c]]) | |
mask = mask.reshape((-1, mask.shape[-2], mask.shape[-1])) | |
out = [] | |
for m in mask: | |
output = m.numpy() | |
for _ in range(abs(expand)): | |
if expand < 0: | |
output = scipy.ndimage.grey_erosion(output, footprint=kernel) | |
else: | |
output = scipy.ndimage.grey_dilation(output, footprint=kernel) | |
output = torch.from_numpy(output) | |
out.append(output) | |
return torch.stack(out, dim=0) | |
def parse_string_to_list(s): | |
elements = s.split(',') | |
result = [] | |
def parse_number(s): | |
try: | |
if '.' in s: | |
return float(s) | |
else: | |
return int(s) | |
except ValueError: | |
return 0 | |
def decimal_places(s): | |
if '.' in s: | |
return len(s.split('.')[1]) | |
return 0 | |
for element in elements: | |
element = element.strip() | |
if '...' in element: | |
start, rest = element.split('...') | |
end, step = rest.split('+') | |
decimals = decimal_places(step) | |
start = parse_number(start) | |
end = parse_number(end) | |
step = parse_number(step) | |
current = start | |
if (start > end and step > 0) or (start < end and step < 0): | |
step = -step | |
while current <= end: | |
result.append(round(current, decimals)) | |
current += step | |
else: | |
result.append(round(parse_number(element), decimal_places(element))) | |
return result |