/
despat_double_exposure_separation.py
104 lines (80 loc) · 2.5 KB
/
despat_double_exposure_separation.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
import os
import shutil
from datetime import datetime
INPUT_FOLDER = "/Volumes/ctdrive/export_koerbersee2"
OUTPUT_FOLDER = os.path.join(INPUT_FOLDER, "2nd")
# EXTENSION = ".jpg"
EXTENSION = ".tif"
MAX_TIME_DIFF = 3000
images = []
double_exposure_images = []
def _sort_helper(d):
# still_123.jpg
value = d[1]
if value.startswith("still_"):
pos = value.index(".")
number = value[6:pos]
return int(number)
elif value.startswith("DSCF"):
pos = value.index(".")
number = value[4:pos]
return int(number)
elif value.startswith("DSC"):
pos = value.index(".")
number = value[3:pos]
return int(number)
elif value.startswith("DJI_"):
pos = value.index(".")
number = value[4:pos]
return int(number)
elif "_" in value:
pos = value.index("_")
number = value[0:pos]
return int(number)
else:
try:
filename = os.path.splitext(value)[0]
return int(filename)
except ValueError as e:
return 0
# recursive with sub directories
# for root, dirs, files in os.walk(INPUT_FOLDER):
# for f in files:
# if f.lower().endswith(EXTENSION):
# images.append((root, f))
# non-recursive
for f in os.listdir(INPUT_FOLDER):
if f.lower().endswith(EXTENSION):
images.append((INPUT_FOLDER, f))
if len(images) < 2:
print("found {} images. abort.".format(len(images)))
exit()
else:
print("found {} images in total.".format(len(images)))
if not os.path.exists(OUTPUT_FOLDER):
os.makedirs(OUTPUT_FOLDER)
print("created output dir: {}".format(OUTPUT_FOLDER))
images = sorted(images, key=_sort_helper)
# for img in images:
# print(img[1])
# exit()
prev = None
for img in images:
timestamp = os.path.splitext(img[1])[0]
if "_" in timestamp:
timestamp = timestamp[0:timestamp.rfind("_")]
timestamp = int(timestamp)
# print(timestamp)
if prev is not None:
diff = timestamp - prev[0]
if diff < MAX_TIME_DIFF:
double_exposure_images.append((prev[1], img))
# print(diff)
prev = (timestamp, img)
print("found {} double exposure images.".format(len(double_exposure_images)))
# save the 2nd file with the filename of the prev file in the output folder
for img in double_exposure_images:
src = os.path.join(img[1][0], img[1][1])
dst = os.path.join(OUTPUT_FOLDER, img[0][1])
shutil.move(src, dst)
# print(double_exposure_images)