-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhdr_brackets.py
408 lines (340 loc) · 13.5 KB
/
hdr_brackets.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import sys
import subprocess
import json
import pathlib
import exifread
from math import log
from tkinter import *
from tkinter import filedialog, messagebox, ttk
from concurrent.futures import ThreadPoolExecutor
import threading
from time import sleep
import http.client
import urllib
if getattr(sys, 'frozen', False):
SCRIPT_DIR = pathlib.Path(sys.executable).parent # Built with cx_freeze
else:
SCRIPT_DIR = pathlib.Path(__file__).resolve().parent
def center(win):
win.update_idletasks()
width = win.winfo_width()
height = win.winfo_height()
x = (win.winfo_screenwidth() // 2) - (width // 2)
# Add 32 to account for titlebar & borders
y = (win.winfo_screenheight() // 2) - (height+32 // 2)
win.geometry('{}x{}+{}+{}'.format(width, height, x, y))
def read_json(fp: pathlib.Path) -> dict:
with fp.open('r') as f:
s = f.read()
# Work around invalid JSON when people paste single backslashes in there.
s = s.replace('\\', '/')
try:
return json.loads(s)
except json.JSONDecodeError as ex:
raise RuntimeError('Error reading JSON from %s: %s' % (fp, ex))
def get_exe_paths() -> dict:
global SCRIPT_DIR
cf = SCRIPT_DIR / 'exe_paths.json'
default_exe_paths = {
"blender_exe": "",
"luminance_cli_exe": "",
"align_image_stack_exe": ""
}
exe_paths = {}
error = ""
missing_json_error = "You need to configure some paths first. Edit the '%s' file and fill in the paths." % cf
if not cf.exists() or cf.stat().st_size == 0:
with cf.open('w') as f:
json.dump(default_exe_paths, f, indent=4, sort_keys=True)
error = missing_json_error + ' (file does not exist or is empty)'
else:
exe_paths = read_json(cf)
for key, path in exe_paths.items():
if not path:
error = missing_json_error + ' (%s is empty)' % key
break
if not pathlib.Path(path).exists():
error = "\"%s\" in exe_paths.json either doesn't exist or is an invalid path." % path
if error:
print(error)
input("Press enter to exit.")
sys.exit(0)
return exe_paths
EXE_PATHS = get_exe_paths()
def play_sound(sf: str):
if pathlib.Path(sf).exists():
try:
from winsound import PlaySound, SND_FILENAME
except ImportError:
pass
else:
PlaySound(sf, SND_FILENAME)
def notify_phone(msg="Done"):
pushover_cfg_f = SCRIPT_DIR / 'pushover.json'
if not pushover_cfg_f.exists():
return
pushover_cfg = read_json(pushover_cfg_f)
conn = http.client.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.parse.urlencode({
"token": pushover_cfg['token'],
"user": pushover_cfg['user'],
"message": msg,
}), {"Content-type": "application/x-www-form-urlencoded"})
conn.getresponse()
def chunks(l, n):
if n < 1:
n = 1
return [l[i:i + n] for i in range(0, len(l), n)]
def get_exif(filepath: pathlib.Path):
with filepath.open('rb') as f:
tags = exifread.process_file(f)
resolution = str(tags["Image ImageWidth"]) + 'x' + \
str(tags["Image ImageLength"])
shutter_speed = eval(str(tags["EXIF ExposureTime"]))
try:
aperture = eval(str(tags["EXIF FNumber"]))
except ZeroDivisionError:
aperture = 0
iso = int(str(tags["EXIF ISOSpeedRatings"]))
return {"resolution": resolution, "shutter_speed": shutter_speed, "aperture": aperture, "iso": iso}
def ev_diff(bright_image, dark_image):
dr_shutter = log(bright_image['shutter_speed'] /
dark_image['shutter_speed'], 2)
try:
dr_aperture = log(dark_image['aperture'] /
bright_image['aperture'], 1.41421)
except (ValueError, ZeroDivisionError):
# No lens data means aperture is 0, and we can't divide by 0 :)
dr_aperture = 0
dr_iso = log(bright_image['iso']/dark_image['iso'], 2)
return dr_shutter + dr_aperture + dr_iso
class HDRBrackets(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.initUI()
def initUI(self):
self.master.title("HDR Brackets")
self.pack(fill=BOTH, expand=True)
padding = 8
self.buttons_to_disable = []
clipboard = ''
try:
clipboard = Frame.clipboard_get(self)
except TclError:
pass
# ========== Input ==========
r1 = Frame(master=self)
initial_label = "Select a folder..."
if clipboard: # if a path is copied in clipboard, fill it in automatically
try:
clippath = pathlib.Path(clipboard)
if clippath.exists():
initial_label = clipboard
except OSError:
pass # Not a valid path.
lbl_input = Label(r1, text="Input Folder:")
lbl_input.pack(side=LEFT, padx=(padding, 0))
self.input_folder = Entry(r1)
self.input_folder.insert(0, initial_label)
self.input_folder.pack(side=LEFT, fill=X, expand=True, padx=padding)
self.buttons_to_disable.append(self.input_folder)
btn_browse = Button(r1, text='Browse', command=self.set_input_folder)
btn_browse.pack(side=RIGHT, padx=(0, padding))
self.buttons_to_disable.append(btn_browse)
r1.pack(fill=X, pady=(padding, 0))
r2 = Frame(master=self)
lbl_pattern = Label(r2, text="Matching Pattern:")
lbl_pattern.pack(side=LEFT, padx=(padding, 0))
self.extension = Entry(r2, width=6)
self.extension.insert(0, ".tif")
self.extension.pack(side=LEFT, padx=(padding/2, 0))
self.buttons_to_disable.append(self.extension)
lbl_threads = Label(r2, text="Threads:")
lbl_threads.pack(side=LEFT, padx=(padding, 0))
self.num_threads = Spinbox(r2, from_=1, to=9999999, width=2)
self.num_threads.delete(0, "end")
self.num_threads.insert(0, "6")
self.num_threads.pack(side=LEFT, padx=(padding/3, 0))
self.buttons_to_disable.append(self.num_threads)
self.do_align = BooleanVar()
lbl_align = Label(r2, text="Align:")
lbl_align.pack(side=LEFT, padx=(padding, 0))
self.align = Checkbutton(
r2, variable=self.do_align, onvalue=True, offvalue=False)
self.align.pack(side=LEFT)
self.buttons_to_disable.append(self.align)
self.btn_execute = Button(r2, text='Create HDRs', command=self.execute)
self.btn_execute.pack(side=RIGHT, fill=X, expand=True, padx=padding)
self.buttons_to_disable.append(self.btn_execute)
r2.pack(fill=X, pady=(padding, 0))
r3 = Frame(master=self)
self.progress = ttk.Progressbar(
r3, orient=HORIZONTAL, length=100, mode='determinate')
self.progress.pack(fill=X)
r3.pack(fill=X, pady=(padding, 0))
def set_input_folder(self):
path = filedialog.askdirectory()
if path:
self.input_folder.delete(0, END)
self.input_folder.insert(0, path)
self.btn_execute['text'] = "Create HDRs"
self.btn_execute['command'] = self.execute
self.progress['value'] = 0
def do_merge(self, blender_exe: str,
merge_blend: pathlib.Path, merge_py: pathlib.Path,
exifs, out_folder: pathlib.Path,
filter_used, i, img_list, folder: pathlib.Path, luminance_cli_exe,
align_image_stack_exe):
exr_folder = out_folder / 'exr'
jpg_folder = out_folder / 'jpg'
align_folder = out_folder / 'aligned'
exr_folder.mkdir(parents=True, exist_ok=True)
jpg_folder.mkdir(parents=True, exist_ok=True)
exr_path = exr_folder / ('merged_%03d.exr' % i)
jpg_path = jpg_folder / exr_path.with_suffix('.jpg').name
if exr_path.exists():
print("Skipping set %d, %s exists" % (i, exr_path))
return
if self.do_align.get():
print("Aligning", i)
align_folder.mkdir(parents=True, exist_ok=True)
actual_img_list = [i.split("___")[0] for i in img_list]
cmd = [
align_image_stack_exe,
'-i',
'-l',
'-a',
(align_folder / "align_{}_".format(i)).as_posix(),
'--gpu',
]
cmd += actual_img_list
new_img_list = []
for j, img in enumerate(img_list):
new_img_list.append((align_folder / "align_{}_{}.tif___{}".format(
i,
str(j).zfill(4),
img_list[j].split('___')[-1]
)).as_posix())
subprocess.check_call(cmd)
img_list = new_img_list
print("Merging", i)
cmd = [
blender_exe,
'--background',
merge_blend.as_posix(),
'--factory-startup',
'--python',
merge_py.as_posix(),
'--',
exifs[0]['resolution'],
exr_path.as_posix(),
filter_used,
]
cmd += img_list
subprocess.check_call(cmd)
cmd = [
luminance_cli_exe,
'-l',
exr_path.as_posix(),
'-t',
'reinhard02',
'-q',
'98',
'-o',
jpg_path.as_posix(),
]
subprocess.check_call(cmd)
def execute(self):
def real_execute():
folder = pathlib.Path(self.input_folder.get())
if not folder.exists():
messagebox.showerror(
"Folder does not exist", "The input path you have selected does not exist!")
return
print("Starting...")
self.btn_execute['text'] = "Busy..."
self.progress['value'] = 0
for btn in self.buttons_to_disable:
btn['state'] = 'disabled'
global EXE_PATHS
global SCRIPT_DIR
blender_exe = EXE_PATHS['blender_exe']
luminance_cli_exe = EXE_PATHS['luminance_cli_exe']
align_image_stack_exe = EXE_PATHS['align_image_stack_exe']
merge_blend = SCRIPT_DIR / "blender" / "HDR_Merge.blend"
merge_py = SCRIPT_DIR / "blender" / "blender_merge.py"
out_folder = folder / "Merged"
glob = self.extension.get()
if '*' not in glob:
glob = '*%s' % glob
files = list(folder.glob(glob))
# Analyze EXIF to determine number of brackets
exifs = []
for f in files:
e = get_exif(f)
if e in exifs:
break
exifs.append(e)
brackets = len(exifs)
print("\nBrackets:", brackets)
print("Exifs:", exifs)
evs = [ev_diff({"shutter_speed": 1000000000, "aperture": 0.1,
"iso": 1000000000000}, e) for e in exifs]
evs = [ev-min(evs) for ev in evs]
filter_used = "None" # self.filter.get().replace(' ', '').replace('+', '_') # Depreciated
# Do merging
executor = ThreadPoolExecutor(
max_workers=int(self.num_threads.get()))
threads = []
sets = chunks(files, brackets)
print("Sets:", len(sets), "\n")
for i, s in enumerate(sets):
img_list = []
for ii, img in enumerate(s):
img_list.append(img.as_posix()+'___'+str(evs[ii]))
# self.do_merge (blender_exe, merge_blend, merge_py, exifs, out_folder, filter_used, i, img_list, folder, luminance_cli_exe)
t = executor.submit(self.do_merge, blender_exe, merge_blend, merge_py, exifs, out_folder,
filter_used, i, img_list, folder, luminance_cli_exe, align_image_stack_exe)
threads.append(t)
while any(not t.done() for t in threads):
sleep(2)
self.update()
num_finished = 0
for tt in threads:
if not tt.done():
continue
try:
tt.result()
except Exception as ex:
print('Exception in thread: %s', ex)
num_finished += 1
progress = (num_finished/len(threads))*100
print("Progress:", progress)
self.progress['value'] = int(progress)
print("Done!!!")
notify_phone(folder)
for btn in self.buttons_to_disable:
btn['state'] = 'normal'
self.btn_execute['text'] = "Done!"
self.btn_execute['command'] = self.quit
play_sound("C:/Windows/Media/Speech On.wav")
self.update()
# Run in a separate thread to keep UI alive
threading.Thread(target=real_execute).start()
def quit(self):
global root
root.destroy()
def main():
print("This window will report detailed progress of the blender renders.")
print("Use the other window to start the merging process.")
global root
root = Tk()
root.geometry("450x86")
center(root)
root.iconbitmap(str(SCRIPT_DIR / "icons/icon.ico"))
app = HDRBrackets(root)
root.mainloop()
if __name__ == '__main__':
main()