-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOlyExifSort.py
482 lines (394 loc) · 17.3 KB
/
OlyExifSort.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/usr/bin/python3
import argparse
import re
import exiftool
import os
import fnmatch
import sys
import re
import shutil
import pathlib
from collections import defaultdict
from dataclasses import dataclass
from enum import Enum, IntEnum
class DriveMode(IntEnum):
SingleShot = 0
ContinuousShooting = 1
ExposureBracketing = 2
WhiteBalanceBracketing = 3
ExposureWBBracketing = 4
Bracketing = 5
class DriveModeBytes(IntEnum):
Mode = 0
ShotNum = 1
ModeBits = 2
ShutterMode = 4
class BrktMode(IntEnum):
NONE = 0x00
AE = 0x01
WB = 0x02
FL = 0x04
MF = 0x08
ISO = 0x10
AEA = 0x20
FOC = 0x40
class StackedImage(Enum):
NO = '0 0'
LiveComposite = '1 *'
LiveTimeBulb = '4 *'
ND2 = '3 2'
ND4 = '3 4'
ND8 = '3 8'
ND16 = '3 16'
ND32 = '3 32'
HDR1 = '5 4'
HDR2 = '6 4'
TripodHR = '8 8'
FocusStacked = '9 *'
HandheldHR = '11 16'
@dataclass
class BrktSequenceEntry:
driveMode: DriveMode
brktMode: BrktMode
stackedImage: StackedImage
num: int
file: list()
class ReturnStatus(IntEnum):
SUCCESS = 0
ERROR = 1
INVALID_PATH = 2
NO_SEQUENCES_FOUND = 3
NO_IMAGES_FOUND = 4
NO_FILES = 5
def create_build_parser(parser):
"""
configures/adds the relevant options to the parser
:param parser:
:return:
"""
parser.add_argument("-p", "--path", default="", metavar="<PATH>",
help="specifies the path which should be sorted")
parser.add_argument("-cHDR", "--countOffsetHDR", default="0", metavar="<count offset>",
help="set the count offset for HDR sequence folders")
parser.add_argument("-cFOC", "--countOffsetFOC", default="0", metavar="<count offset>",
help="set the count offset for FOC sequence folders")
parser.add_argument("-cAE", "--countOffsetAE", default="0", metavar="<count offset>",
help="set the count offset for AE sequence folders")
parser.add_argument("-cWB", "--countOffsetWB", default="0", metavar="<count offset>",
help="set the count offset for WB sequence folders")
parser.add_argument("-cFL", "--countOffsetFL", default="0", metavar="<count offset>",
help="set the count offset for FL sequence folders")
parser.add_argument("-cMF", "--countOffsetMF", default="0", metavar="<count offset>",
help="set the count offset for MF sequence folders")
parser.add_argument("-cISO", "--countOffsetISO", default="0", metavar="<count offset>",
help="set the count offset for ISO sequence folders")
return parser
def parse_command_line_arguments(argv):
"""
parse command line arguments
:return: dictionary containing the arguments (from argparse module)
"""
parser = argparse.ArgumentParser(
description="this program sorts and moves AE- and Focus-Bracketing sequences"
)
parser = create_build_parser(parser)
args = parser.parse_args(argv)
return args
def groupBracketing(metadata):
aeaBrkt = list()
focBrkt = list()
aeBrkt = list()
wbBrkt = list()
flBrkt = list()
mfBrkt = list()
isoBrkt = list()
sequence = list()
stackedImage = StackedImage.NO
for e in metadata:
if e["File:FileType"] == "ORF" or e["File:FileType"] == "JPEG":
# extract drive-mode bytes
driveModeBytes = [int(k)
for k in e["MakerNotes:DriveMode"].split(" ")]
# extract stacked-image type
for si in StackedImage:
match = bool(re.match(si.value, e["MakerNotes:StackedImage"]))
if match:
stackedImage = si
break
# set stacked-image type
if stackedImage == StackedImage.FocusStacked:
sequence.append(BrktSequenceEntry(
DriveMode(driveModeBytes[DriveModeBytes.Mode]),
BrktMode(driveModeBytes[DriveModeBytes.ModeBits]),
stackedImage, driveModeBytes[DriveModeBytes.ShotNum],
e))
# check if image is part of a bracketing sequence
if driveModeBytes[DriveModeBytes.Mode] == DriveMode.Bracketing:
entry = BrktSequenceEntry(
DriveMode(driveModeBytes[DriveModeBytes.Mode]),
BrktMode(driveModeBytes[DriveModeBytes.ModeBits]),
stackedImage, driveModeBytes[DriveModeBytes.ShotNum],
e)
if len(sequence) > 0:
if (entry.num == 1 or entry.num != (sequence[-1].num + 1)) and \
len(sequence) > 0 and \
entry.file["File:FileName"].split(".")[0] != sequence[-1].file["File:FileName"].split(".")[0]:
if sequence[0].brktMode == BrktMode.AEA:
aeaBrkt.append(sequence)
if sequence[0].brktMode == BrktMode.FOC:
focBrkt.append(sequence)
if sequence[0].brktMode == BrktMode.AE:
aeBrkt.append(sequence)
if sequence[0].brktMode == BrktMode.WB:
wbBrkt.append(sequence)
if sequence[0].brktMode == BrktMode.FL:
flBrkt.append(sequence)
if sequence[0].brktMode == BrktMode.MF:
mfBrkt.append(sequence)
if sequence[0].brktMode == BrktMode.ISO:
isoBrkt.append(sequence)
sequence = list()
if entry.brktMode == BrktMode.AEA or entry.brktMode == BrktMode.FOC:
sequence.append(entry)
# Needs to be done to add the last found sequence
if len(sequence) > 0:
if sequence[0].brktMode == BrktMode.AEA:
aeaBrkt.append(sequence)
if sequence[0].brktMode == BrktMode.FOC:
focBrkt.append(sequence)
if sequence[0].brktMode == BrktMode.AE:
aeBrkt.append(sequence)
if sequence[0].brktMode == BrktMode.WB:
wbBrkt.append(sequence)
if sequence[0].brktMode == BrktMode.FL:
flBrkt.append(sequence)
if sequence[0].brktMode == BrktMode.MF:
mfBrkt.append(sequence)
if sequence[0].brktMode == BrktMode.ISO:
isoBrkt.append(sequence)
return aeaBrkt, focBrkt, aeBrkt, wbBrkt, flBrkt, mfBrkt, isoBrkt
def moveBracketing(moveList, path, mode, minCountOffset=0):
mainDirLabel = ""
subDirLabel = ""
if mode == BrktMode.AEA:
mainDirLabel = "HDRs"
subDirLabel = "HDR"
if mode == BrktMode.FOC:
mainDirLabel = "FOCs"
subDirLabel = "FOC"
if mode == BrktMode.AE:
mainDirLabel = "AEs"
subDirLabel = "AE"
if mode == BrktMode.WB:
mainDirLabel = "WBs"
subDirLabel = "WB"
if mode == BrktMode.FL:
mainDirLabel = "FLs"
subDirLabel = "FL"
if mode == BrktMode.MF:
mainDirLabel = "MFs"
subDirLabel = "MF"
if mode == BrktMode.ISO:
mainDirLabel = "ISOs"
subDirLabel = "ISO"
print("Move %s Bracketing Sequences" % (subDirLabel))
mainDirName = os.path.basename(path)
targetMainDirName = mainDirName + "_" + mainDirLabel
targetMainDirPath = os.path.join(path, targetMainDirName)
subDirNameMask = mainDirName + "_" + subDirLabel + "_"
moveBrktCount = 0
moveBrktCountOffset = 0
if os.path.exists(targetMainDirPath):
print("preexisting folder '" + targetMainDirName + "' found")
existingBrktFolders = fnmatch.filter(
os.listdir(targetMainDirPath), subDirNameMask + '*')
if (len(existingBrktFolders) > 0):
existingBrktFolders.sort(reverse=True)
moveBrktCountOffset = int(
existingBrktFolders[0].replace(subDirNameMask, ''))
if minCountOffset > moveBrktCountOffset:
moveBrktCountOffset = minCountOffset
if not os.path.exists(targetMainDirPath):
os.makedirs(targetMainDirPath)
for seq in moveList:
pathExists = True
# Create sub-directory in hdr-directory if it does not already exists
# if directory already exists, try next higher number for directory
while pathExists:
moveBrktCount += 1
dirname = "%s%03d" % (
subDirNameMask, moveBrktCount + moveBrktCountOffset)
targetDirPath = os.path.join(targetMainDirPath, dirname)
pathExists = os.path.exists(targetDirPath)
isLastImageStacked = False
if mode == BrktMode.FOC:
isLastImageStacked = seq[-1].stackedImage.name == "FocusStacked"
if isLastImageStacked:
lastImg = seq[-1]
sourcePath = lastImg.file["SourceFile"]
targetPath = os.path.join(
targetDirPath, lastImg.file["File:FileName"])
fileEnding = pathlib.Path(lastImg.file["File:FileName"]).suffix
targetPathCopy = "%s_%s_%03d%s" % (os.path.join(targetMainDirPath, lastImg.file["File:FileName"].replace(
fileEnding, "")), subDirLabel, moveBrktCount + moveBrktCountOffset, fileEnding)
print("Makedir: %s" % (dirname))
os.makedirs(targetDirPath)
shutil.copy2(sourcePath, targetPathCopy)
for img in seq:
sourcePath = img.file["SourceFile"]
targetPath = os.path.join(
targetDirPath, img.file["File:FileName"])
if not isLastImageStacked:
fileEnding = pathlib.Path(img.file["File:FileName"]).suffix
# Create copy path for the first image from the Sequence
# -> the script copies the first image from the Sequence to the HDR/FOC-Main-Folder and appends the folder name to the file name
# -> makes it easier to look through the Sequences and to identify which folder has the remaining images from the sequence
targetPathCopy = "%s_%s_%03d%s" % (os.path.join(targetMainDirPath, img.file["File:FileName"].replace(
fileEnding, "")), subDirLabel, moveBrktCount + moveBrktCountOffset, fileEnding)
# if target directory does not exists create it and copy the first image from the HDR-Sequence
if not os.path.exists(targetDirPath):
print("Makedir: %s" % (dirname))
os.makedirs(targetDirPath)
shutil.copy2(sourcePath, targetPathCopy)
isLastImageStacked = True
# Move the Sequence to the folder and inform the user about it
print("Move: " + img.file["File:FileName"])
# move image to target directory
shutil.move(sourcePath, targetPath)
print(" ")
def executeExifRead(path):
retStatus = ReturnStatus.ERROR
aeaBrkt = list()
focBrkt = list()
aeBrkt = list()
wbBrkt = list()
flBrkt = list()
mfBrkt = list()
isoBrkt = list()
with exiftool.ExifTool() as et:
# SourceFile
# File:Filename
# MakerNotes:DriveMode
if not os.path.exists(path):
retStatus = ReturnStatus.INVALID_PATH
print(f'given path "{path}" is invalid!')
print(f'aborting!')
else:
numOfFiles = next(os.walk(path))[2]
print(f'Number of Files: {len(numOfFiles)}')
if len(numOfFiles) != 0:
print(f'scanning image EXIF-Data...')
print(f'Please Note: Depending on the number of images, pc performance and storage rw speed this takes some time! Even up to a couple of minutes...')
metadata = et.execute_json(
'-filename', '-DriveMode', '-FileType', '-StackedImage', path)
# sort metadata list by filename
metadata.sort(key=lambda x: x["File:FileName"])
print(f'scanning image EXIF-Data finished!')
if len(metadata) == 0:
retStatus = ReturnStatus.NO_IMAGES_FOUND
print(f'no images found!')
else:
print(f'start grouping images to sequences...')
try:
aeaBrkt, focBrkt, aeBrkt, wbBrkt, flBrkt, mfBrkt, isoBrkt = groupBracketing(
metadata)
print(f'HDR sequences found: ' + str(len(aeaBrkt)))
print(f'FOC sequences found: ' + str(len(focBrkt)))
print(f'AE sequences found: ' + str(len(aeBrkt)))
print(f'WB sequences found: ' + str(len(wbBrkt)))
print(f'FL sequences found: ' + str(len(flBrkt)))
print(f'MF sequences found: ' + str(len(mfBrkt)))
print(f'ISO sequences found: ' + str(len(isoBrkt)))
print("")
printSequences(aeaBrkt, "HDR")
printSequences(focBrkt, "FOC")
printSequences(aeBrkt, "AE")
printSequences(wbBrkt, "WB")
printSequences(flBrkt, "FL")
printSequences(mfBrkt, "MF")
printSequences(isoBrkt, "ISO")
if (len(aeaBrkt) > 0 or len(focBrkt) > 0 or len(aeBrkt) > 0 or len(wbBrkt) > 0 or len(flBrkt) > 0 or len(mfBrkt) > 0 or len(isoBrkt) > 0):
retStatus = ReturnStatus.SUCCESS
else:
retStatus = ReturnStatus.NO_SEQUENCES_FOUND
except KeyError:
print("An KeyError exception occurred!")
retStatus = ReturnStatus.ERROR
except:
print("An exception occurred!")
retStatus = ReturnStatus.ERROR
else:
retStatus = ReturnStatus.NO_FILES
print(f'There are no files in this folder!')
return retStatus, aeaBrkt, focBrkt, aeBrkt, wbBrkt, flBrkt, mfBrkt, isoBrkt
def printSequences(seq, name):
if len(seq):
print(f'# {name} Sequences:')
seqCnt = 0
for gr in seq:
seqCnt += 1
print(f'{name} #{seqCnt}')
for el in gr:
print(
f'{el.file["File:FileName"]}: {el.brktMode.name} {el.driveMode.name} #{el.num}')
print('')
def moveSequences(path, aeaBrkt, focBrkt, aeBrkt, wbBrkt, flBrkt, mfBrkt, isoBrkt, countOffsetHDR=0, countOffsetFOC=0, countOffsetAE=0, countOffsetWB=0, countOffsetFL=0, countOffsetMF=0, countOffsetISO=0):
if len(aeaBrkt) > 0:
moveBracketing(aeaBrkt, path, BrktMode.AEA, int(countOffsetHDR))
print("Moving AEA-Bracketing Sequences Finished!")
print("")
if len(focBrkt) > 0:
moveBracketing(focBrkt, path, BrktMode.FOC, int(countOffsetFOC))
print("Moving FOC-Bracketing Sequences Finished!")
print("")
if len(aeBrkt) > 0:
moveBracketing(aeBrkt, path, BrktMode.AE, int(countOffsetAE))
print("Moving AE-Bracketing Sequences Finished!")
print("")
if len(wbBrkt) > 0:
moveBracketing(wbBrkt, path, BrktMode.WB, int(countOffsetWB))
print("Moving WB-Bracketing Sequences Finished!")
print("")
if len(flBrkt) > 0:
moveBracketing(flBrkt, path, BrktMode.FL, int(countOffsetFL))
print("Moving FL-Bracketing Sequences Finished!")
print("")
if len(mfBrkt) > 0:
moveBracketing(mfBrkt, path, BrktMode.MF, int(countOffsetMF))
print("Moving MF-Bracketing Sequences Finished!")
print("")
if len(isoBrkt) > 0:
moveBracketing(isoBrkt, path, BrktMode.ISO, int(countOffsetISO))
print("Moving ISO-Bracketing Sequences Finished!")
print("")
print("")
print("SUCCESS!")
def main_build(args, params):
retStatus, aeaBrkt, focBrkt, aeBrkt, wbBrkt, flBrkt, mfBrkt, isoBrkt = executeExifRead(
args.path)
status = ""
if (retStatus == ReturnStatus.SUCCESS):
status = "Search for Sequences finished!"
elif (retStatus == ReturnStatus.ERROR):
status = "Search for Sequences ended with an unknown Error!"
elif (retStatus == ReturnStatus.INVALID_PATH):
status = "Given Path is invalid!"
elif (retStatus == ReturnStatus.NO_IMAGES_FOUND):
status = "No images found in given folder!"
elif (retStatus == ReturnStatus.NO_FILES):
status = "No files found in given folder!"
elif (retStatus == ReturnStatus.NO_SEQUENCES_FOUND):
status = "No Bracketing-Sequences found in given folder!"
print(status)
if (len(aeaBrkt) > 0 or len(focBrkt) > 0 or len(aeBrkt) > 0 or len(wbBrkt) > 0 or len(flBrkt) > 0 or len(mfBrkt) > 0 or len(isoBrkt) > 0):
moveSequences(args.path, aeaBrkt, focBrkt, aeBrkt, wbBrkt, flBrkt, mfBrkt, isoBrkt,
args.countOffsetHDR, args.countOffsetFOC, args.countOffsetAE, args.countOffsetWB, args.countOffsetFL, args.countOffsetMF, args.countOffsetISO)
def main(argv):
"""
main routine
"""
params = defaultdict(list)
args = parse_command_line_arguments(argv)
return main_build(args, params)
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))