forked from maagic6/SDIMV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.py
205 lines (195 loc) · 9.29 KB
/
image.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from PIL import Image, ExifTags
import re, filetype, json
from mutagen.mp4 import MP4
class imageProcess:
def __init__(self, fn):
ft = filetype.guess(fn)
self.data = {"prompt": "",
"negative_prompt": "",
"steps": "",
"sampler": "",
"cfg_scale": "",
"seed": "",
"size": "",
"model_hash": "",
"model": "",
"lora": ""}
if ft == None:
self.compatible = False
elif ft.extension in ['png']:
self.image = Image.open(fn)
self.compatible = False
if 'parameters' in self.image.info: #web ui
self.info = str(self.image.info['parameters'])
self.metadataType = 'parameters'
self.compatible = True
self.image.close()
del self.image
elif 'Comment' in self.image.info: #novelai
self.info = json.loads(self.image.info['Comment'])
self.metadataType = 'comment'
self.compatible = True
self.image.close()
self.image = None
del self.image
elif 'prompt' in self.image.info: #comfyui
self.info = json.loads(self.image.info['prompt'])
self.metadataType = 'prompt'
self.compatible = True
self.image.close()
self.image = None
del self.image
elif ft.extension == 'jpg':
self.image = Image.open(fn)
self.compatible = False
exif_data = self.image._getexif()
if exif_data is not None:
for tag, value in ExifTags.TAGS.items():
if tag in exif_data:
if ExifTags.TAGS[tag] == "UserComment":
user_comment = exif_data[tag]
user_comment_unicode = user_comment.decode("utf-8") #decode
user_comment_unicode_sanitized = user_comment_unicode.replace('UNICODE', '').replace('\x00', '')
self.info = user_comment_unicode_sanitized
self.metadataType = 'parameters'
self.compatible = True
self.image.close()
self.image = None
del self.image
elif ft.extension in ['mp4']:
video = MP4(fn)
self.data = {}
try:
if '\xa9cmt' in video.tags:
metadata = video.tags['\xa9cmt']
self.metadataType = "video"
self.info = json.loads(metadata[0])
self.compatible = True
video = None
del video
else:
self.compatible = False
video = None
del video
except:
self.compatible = False
video = None
del video
else:
self.compatible = False
def findKeyName(self, data, keys):
if isinstance(data, dict):
for key, value in data.items():
if key == keys:
return value
result = self.findKeyName(value, keys)
if result is not None:
return result
elif isinstance(data, list):
for item in data:
result = self.findKeyName(item, keys)
if result is not None:
return result
return None
def getInfo(self): # messy
if self.metadataType == 'parameters':
matches = re.findall(r'([^:,]+): ([^,]+)', self.info.replace('\n', ','))
for match in matches:
key = match[0].strip().lower().replace(' ', '_')
value = match[1].strip()
self.data[key] = value
try:
positive = str(re.split(r'Negative prompt: |Steps: ', self.info)[0])
except:
positive = ""
self.data["prompt"]=positive
try:
negative = str(re.split(r'Negative prompt: ', self.info, maxsplit=1)[1].split('Steps:')[0].strip())
except:
negative = ""
self.data["negative_prompt"] = negative
loraTags = re.findall(r'<lora:[^>]+>', self.info)
uniqueLoraTags = set(loraTags)
loraString = ' '.join(uniqueLoraTags)
self.data["lora"] = loraString
if "model" not in self.data:
self.data["model"] = ""
return self.data
if self.metadataType == 'comment': #novelai
self.data["prompt"] = str(self.info["prompt"])
self.data["negative_prompt"] = str(self.info["uc"])
self.data["steps"] = str(self.info["steps"])
self.data["sampler"] = str(self.info["sampler"])
self.data["cfg_scale"] = str(self.info["scale"])
self.data["seed"] = str(self.info["seed"])
self.data["size"] = str(self.info["height"])+'x'+str(self.info["width"])
self.data["model"] = ''
self.data["model_hash"] = ''
loraTags = re.findall(r'<lora:[^>]+>', str(self.info))
uniqueLoraTags = set(loraTags)
loraString = ' '.join(uniqueLoraTags)
self.data["lora"] = loraString
return self.data
if self.metadataType == 'video':
promptKey = self.findKeyName(data=self.info, keys="positive")[0]
self.data["prompt"] = self.info.get('prompt', {}).get(f'{promptKey}', {}).get('inputs', {}).get('text')
self.data["negative_prompt"] = self.info.get('prompt', {}).get('320', {}).get('inputs', {}).get('text')
self.data["steps"] = self.info.get('prompt', {}).get('528', {}).get('inputs', {}).get('steps')
self.data["sampler"] = self.info.get('prompt', {}).get('528', {}).get('inputs', {}).get('sampler_name')
self.data["cfg_scale"] = self.info.get('prompt', {}).get('528', {}).get('inputs', {}).get('cfg')
self.data["seed"] = self.info.get('prompt', {}).get('528', {}).get('inputs', {}).get('seed')
self.data["size"] = str(self.info.get('prompt', {}).get('539', {}).get('inputs', {}).get('height'))+'x'+str(self.info.get('prompt', {}).get('539', {}).get('inputs', {}).get('width'))
self.data["model"] = self.info.get('prompt', {}).get('513', {}).get('inputs', {}).get('model_name')
self.data["model_hash"] = ""
self.data["lora"] = ""
return self.data
if self.metadataType == 'prompt':
promptKey = self.findKeyName(data=self.info, keys="positive")
if type(promptKey) == list:
promptKey = self.findKeyName(data=self.info, keys="positive")[0]
if "pre_text" in self.info.get(f'{promptKey}', {}).get('inputs', {}):
self.data["prompt"] = re.search(r'([^--]+)--neg', str(self.info[f'{promptKey}']['inputs']['pre_text'])).group(1)
else:
self.data["prompt"] = self.info[f'{promptKey}']['inputs'].get('text', None)
else:
self.data["prompt"] = promptKey
negativePromptKey = self.findKeyName(data=self.info, keys="negative")
if type(negativePromptKey) == list:
negativePromptKey = self.findKeyName(data=self.info, keys="negative")[0]
if "pre_text" in self.info.get(f'{promptKey}', {}).get('inputs', {}):
self.data["negative_prompt"] = re.search(r'--neg\s*([^\n]+)', str(self.info[f'{negativePromptKey}']['inputs']['pre_text'])).group(1).strip() #I hate ComfyUI
else:
self.data["negative_prompt"] = self.info[f'{negativePromptKey}']['inputs'].get('text', None)
#self.data["negative_prompt"] = type(negativePromptKey)
else:
self.data["negative_prompt"] = negativePromptKey
self.data["steps"] = self.findKeyName(data=self.info, keys="steps")
self.data["sampler"] = self.findKeyName(data=self.info, keys="sampler_name")
self.data["cfg_scale"] = self.findKeyName(data=self.info, keys="cfg")
self.data["seed"] = self.findKeyName(data=self.info, keys="noise_seed") or self.findKeyName(data=self.info, keys="seed")
self.data["size"] = self.findKeyName(data=self.info, keys="resolution")
self.data["model"] = self.findKeyName(data=self.info, keys="ckpt_name")
self.data["model_hash"] = ''
self.data["lora"] = ''
return self.data
def save_metadata(self):
#todo
pass
def getRaw(self):
return self.info
def positivePrompt(self):
if self.compatible == False:
return -1
else:
if self.metadataType == 'parameters':
positive = ""
return positive
if self.metadataType == 'comment':
positive = ""
return positive
'''def negativePrompt(self):
if self.compatible == False:
return -1
else:
negative = ""
return negative'''