-
Notifications
You must be signed in to change notification settings - Fork 0
/
thetacorrector.py
144 lines (103 loc) · 4.22 KB
/
thetacorrector.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
#!/usr/bin/python3
import sys
import os
import subprocess
from exiftool import ExifToolHelper
def get_exif(fn):
ret = {}
i = Image.open(fn)
info = i._getexif()
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
ret[decoded] = value
return ret
'''
def get_exif3(fn):
#img = PIL.Image.open(fn)
for (k,v) in Image.open(fn)._getexif().iteritems():
print('{k} = {v}'.format(k=TAGS.get(k),v=v) )
'''
def get_args():
import argparse
p = argparse.ArgumentParser(description='Geotag one or more photos with location and orientation from GPX file.')
p.add_argument('path', help='Path containing JPG files, or location of one JPG file.')
return p.parse_args()
def progress(count, total, status=''):
bar_len = 60
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.0 * count / float(total), 1)
bar = '=' * filled_len + '-' * (bar_len - filled_len)
sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', status))
sys.stdout.flush() # As suggested by Rom Ruben (see: http://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console/27871113#comment50529068_27871113)
def get_exif_data(image):
"""Returns a dictionary from the exif data of an PIL Image item. Also converts the GPS Tags"""
exif_data = {}
info = image._getexif()
if info:
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
if decoded == "GPSInfo":
gps_data = {}
for t in value:
sub_decoded = GPSTAGS.get(t, t)
gps_data[sub_decoded] = value[t]
exif_data[decoded] = gps_data
else:
exif_data[decoded] = value
return exif_data
def _get_if_exist(data, key):
if key in data:
return data[key]
return None
def print_pto(filepath,new_filepath,pitch,roll):
pto=open('temp.pto', 'w')
line='i w5376 h2688 f4 v360 r'+str(roll)+' p'+str(pitch)+' y0 n"'+filepath+'"'+"\n"
pto.write(line)
line='p w5376 h2688 f2 v360 r0 p0 y0 n"JPEG q97"'
pto.write(line)
pto.close()
def theta_horizont_auto_correction(filepath):
'''
'''
nona_path=r'''C:\Program Files\Hugin2015\bin\nona.exe'''
nona_path = 'nona'
#get photo data
with ExifToolHelper() as et:
metadata = et.get_metadata(filepath)
try:
pitch = metadata[0]['XMP:PosePitchDegrees']
roll = metadata[0]['XMP:PoseRollDegrees']
except:
return False
directory = os.path.join(os.path.dirname(filepath),'horizont_correction')
if not os.path.exists(directory):
os.makedirs(directory)
#new_filepath=os.path.join(os.path.dirname(filepath),os.path.splitext(os.path.basename(filepath))[0]+'_hor'+'.jpg')
new_filepath=os.path.join(directory,os.path.splitext(os.path.basename(filepath))[0]+'_hor'+'.jpg')
print_pto(filepath,new_filepath,pitch,roll)
run_str='"'+nona_path+'" -o "'+new_filepath+'" temp.pto'
cmd=[nona_path,'-o',new_filepath,'temp.pto']
print(cmd)
subprocess.run(cmd)
#-tagsFromFile source_image.jpg -XMP:All= -ThumbnailImage= -m destination_image.jpg
s_params=' -tagsFromFile "%s"' % filepath+' -overwrite_original -XMP:PosePitchDegrees=0 -XMP:PoseRollDegrees=0 -quiet -ThumbnailImage= -m "%s"' % new_filepath
cmd = 'exiftool '+s_params
os.system(cmd)
if __name__ == '__main__':
args = get_args()
if args.path.lower().endswith(".jpg"):
# single file
file_list = [args.path]
else:
file_list = []
for root, sub_folders, files in os.walk(args.path):
file_list += [os.path.join(root, filename) for filename in files if filename.lower().endswith(".jpg") and 'horizont_correction' not in os.path.join(root, filename)]
print("===\nStarting update of {0} images .\n===".format(len(file_list)))
index = 0
for filepath in file_list:
total = len(file_list)
index = index+1
if index > total:
index=total
progress(index, total, status='Add information to '+str(total) + ' photos')
theta_horizont_auto_correction(filepath)