-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathload_image_node.py
More file actions
89 lines (75 loc) · 3.21 KB
/
load_image_node.py
File metadata and controls
89 lines (75 loc) · 3.21 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
85
86
87
88
89
import os
import json
from PIL import Image, ImageOps
import torch
import numpy as np
try:
import folder_paths
except ImportError:
folder_paths = None
class EricLoadImageWithPrompt:
"""
Loads an image and extracts generation metadata (prompt) if available.
Supports standard ComfyUI image loading + metadata extraction.
"""
@classmethod
def INPUT_TYPES(s):
input_dir = folder_paths.get_input_directory()
files = [f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f))]
return {
"required": {
"image": (sorted(files), {"image_upload": True})
}
}
CATEGORY = "Eric Prompt Enhancers"
RETURN_TYPES = ("IMAGE", "MASK", "STRING")
RETURN_NAMES = ("IMAGE", "MASK", "PROMPT_TEXT")
FUNCTION = "load_image"
def load_image(self, image):
image_path = folder_paths.get_annotated_filepath(image)
i = Image.open(image_path)
i = ImageOps.exif_transpose(i)
image = i.convert("RGB")
# Process image for ComfyUI (Tensor)
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
# Create mask
if 'A' in i.getbands():
mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0
mask = 1. - torch.from_numpy(mask)
else:
mask = torch.zeros((64,64), dtype=torch.float32, device="cpu")
# Extract Metadata
prompt_text = ""
# 1. Try standard PNG info (parameters/Comment)
# A1111 stores generation info in 'parameters'
if i.info:
if 'parameters' in i.info:
prompt_text = i.info['parameters']
elif 'Comment' in i.info:
# Some software uses Comment
try:
# Try to parse JSON if it looks like it
comment = i.info['Comment']
if comment.strip().startswith('{'):
data = json.loads(comment)
prompt_text = data.get('prompt', comment)
else:
prompt_text = comment
except:
prompt_text = i.info['Comment']
# 2. Try ComfyUI workflow metadata (often in 'workflow' or 'prompt')
# This is complex because it's the whole graph. We try to find a primitive string or positive prompt.
# For now, we'll stick to the text-based 'parameters' which is the standard for sharing prompts.
# Clean up A1111 'parameters' block to get just the positive prompt
# Usually it's: "Positive Prompt\nNegative Prompt: ...\nSteps: ..."
if prompt_text and "Negative prompt:" in prompt_text:
prompt_text = prompt_text.split("Negative prompt:")[0].strip()
elif prompt_text and "Steps:" in prompt_text:
prompt_text = prompt_text.split("Steps:")[0].strip()
return (image, mask, prompt_text)
@classmethod
def IS_CHANGED(s, image):
image_path = folder_paths.get_annotated_filepath(image)
m = os.path.getmtime(image_path)
return m