-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpopup.py
69 lines (54 loc) · 1.99 KB
/
popup.py
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
import psutil
import os
from PIL import Image
import torch
import torchvision.transforms.functional as tf
from pathlib import Path
import tempfile
import folder_paths
node_path = os.path.join(folder_paths.get_folder_paths("custom_nodes")[0], "comfyui-popup_preview")
popup_window_path = os.path.join(node_path, 'window', 'popup_window.py')
python_path = os.path.join(node_path, 'window', 'venv', 'Scripts', 'python.exe')
def openWindow():
if os.path.exists( python_path ):
python_running = any(p.info['exe'] == python_path for p in psutil.process_iter(['pid', 'name', 'exe']))
if os.path.exists( popup_window_path ):
if not python_running:
import subprocess
subprocess.Popen([python_path, popup_window_path])
else:
print(f'popup window not exist on: {popup_window_path}')
else:
print(f'Python not exist on: {python_path} and this is popup window path {popup_window_path}')
def save_image(img: torch.Tensor, subpath):
path = subpath
if len(img.shape) == 4 and img.shape[0] == 1:
img = img.squeeze(0)
if len(img.shape) != 3 or img.shape[2] != 3:
raise ValueError(f"Input image must have 3 channels and a 3-dimensional shape, but got {img.shape}")
img = img.permute(2, 0, 1)
img = img.clamp(0, 1)
img = tf.to_pil_image(img)
img.save(path, format="PNG", compress_level=1)
openWindow()
class PreviewPopup:
INPUT_TYPES = lambda: { "required": { "image": ("IMAGE",) }, }
RETURN_TYPES = ()
OUTPUT_NODE = True
FUNCTION = "execute"
CATEGORY = "ToyxyzTestNodes"
def execute(
self,
image: torch.Tensor
):
assert isinstance(image, torch.Tensor)
OUTPUT_PATH = Path(tempfile.gettempdir()) / 'temp_image_preview.png'
subpath = OUTPUT_PATH
save_image(image, subpath)
return ()
NODE_CLASS_MAPPINGS = {
"PreviewPopup": PreviewPopup
}
NODE_DISPLAY_NAME_MAPPINGS = {
"PreviewPopup": "PreviewPopup"
}