forked from pixaroma/ComfyUI-Pixaroma
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes_crop.py
More file actions
84 lines (67 loc) · 2.65 KB
/
nodes_crop.py
File metadata and controls
84 lines (67 loc) · 2.65 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import torch
import numpy as np
from PIL import Image
import os
import json
import folder_paths
class PixaromaCrop:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"crop_json": ("STRING", {"default": "{}", "multiline": True}),
}
}
RETURN_TYPES = ("IMAGE", "INT", "INT")
RETURN_NAMES = ("IMAGE", "WIDTH", "HEIGHT")
FUNCTION = "load_crop"
CATEGORY = "Pixaroma"
@classmethod
def IS_CHANGED(cls, crop_json):
"""Force re-execution when the cropped file on disk changes."""
if not crop_json or crop_json.strip() in ("", "{}"):
return ""
try:
meta = json.loads(crop_json)
composite_path = meta.get("composite_path", "")
if composite_path:
input_dir = folder_paths.get_input_directory()
full_path = os.path.join(input_dir, composite_path)
if os.path.exists(full_path):
return os.path.getmtime(full_path)
except Exception:
pass
return crop_json
def load_crop(self, crop_json):
empty_image = torch.ones((1, 1024, 1024, 3), dtype=torch.float32)
if not crop_json or crop_json.strip() in ("", "{}"):
return (empty_image, 1024, 1024)
try:
meta = json.loads(crop_json)
if not isinstance(meta, dict):
return (empty_image, 1024, 1024)
doc_w = int(meta.get("doc_w", 1024))
doc_h = int(meta.get("doc_h", 1024))
composite_path = meta.get("composite_path", "")
if not composite_path:
arr = np.ones((doc_h, doc_w, 3), dtype=np.float32)
return (torch.from_numpy(arr)[None,], doc_w, doc_h)
input_dir = os.path.realpath(folder_paths.get_input_directory())
full_path = os.path.realpath(os.path.join(input_dir, composite_path))
if not full_path.startswith(input_dir + os.sep):
print("[PixaromaCrop] Security: composite_path escapes input directory, blocked.")
return (empty_image, doc_w, doc_h)
if not os.path.exists(full_path):
return (empty_image, doc_w, doc_h)
img = Image.open(full_path).convert("RGB")
arr = np.array(img).astype(np.float32) / 255.0
return (torch.from_numpy(arr)[None,], doc_w, doc_h)
except Exception as e:
print(f"[PixaromaCrop] Load error: {e}")
return (empty_image, 1024, 1024)
NODE_CLASS_MAPPINGS = {
"PixaromaCrop": PixaromaCrop,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"PixaromaCrop": "Image Crop Pixaroma",
}