-
Notifications
You must be signed in to change notification settings - Fork 1
/
plump.py
executable file
·1129 lines (929 loc) · 33.9 KB
/
plump.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import os
import shutil
import subprocess
import time
import sys
import re
# Version nr with format <RELEASE>.<MAJOR>.<MINOR> YYYYMMDDHHmm
VERSION = '1.1.8 Build 201901040138'
DIR_00 = '00_Inbox'
DIR_01 = '01_Import'
DIR_02 = '02_Progress'
DIR_FOW = '.fow'
DIR_VIDEO = 'video'
DIR_JPG = 'jpg'
DIR_RAW = 'raw'
DIR_FINAL = 'final'
DIR_WORK = 'work'
BACKUP_PATH = 'backup.path'
TASK = 'task'
TYPE_RAW = 'raw'
TYPE_JPG = 'jpg'
TYPE_TIF = 'tif'
# Kind of allowed arguments: None
NONE_PARAM = 0
# Kind of allowed arguments: optional
OPTIONAL_PARAM = 1
# Kind of allowed arguments: mandatory
MANDATORY_PARAM = 2
# Groups all exports destinations together. The have to start with 'export.'
EXPORT_PREFIX = 'export'
# Groups all exif values together. The have to start with 'exif.'
EXIF_PREFIX = 'exif'
# Groups all gpx keys together. The have to start with 'gpx.'
GPS_PREFIX = 'gps'
# Key for path to gps track folder
GPS_TRACK_PATH = 'gps.tracks'
# Groups all load destinations together. The have to start with 'load.'
LOAD_PREFIX = 'load'
#####################################################################
# Conventions:
# - A 'path' is an absolute path within the system. On the other
# hand is a 'subdir' a subdirectory within a fw.
#####################################################################
def progress_prepare(max_index, action, what):
"""
Prepares progress output and writes to stdout. Returns dict with
needed values. Output: 'Processing <what> <max_index>: 0'. To update
progress use:
sys.stdout.write(ret['back'] + ret['formatting'].format(str(index)))
sys.stdout.flush()
Parameters
----------
max_lines: Integer
Progress maximum, e.g. '123'
action: String
Name of the action, e.g. 'Processing', should start in upper case
what: String
Output text, e.g. 'RAW'
:return: Dictionary with keys 'back' and 'formatting' for output
Example
-------
progress_prepare(123, 'RAW')
return dict(back=3, formatting=' {:<' + str(len(max_index)) + '}'
"""
digits = len(str(max_index))
formatting = ' {:<' + str(digits) + '}'
formatting2 = '{} {} {:<' + str(digits) + '}:' + formatting
sys.stdout.write(formatting2.format(action, what, str(max_index), str(0)))
back = '\b'
for i in str(max_index):
back += '\b'
return dict(back=back, formatting=formatting)
def load_execute(analysis, subdir, dest, processing_flags, verb_present):
"""
Executes the load (copy or move), for a specific kind of image files
(jpg, raw of videos).
analysis: one part of the analyse dict; jpg, raw od video
subdir: e.g. DIR_JPG
dest: destination path
verb_present: text 'move' or 'copy' for printing error
Returns dictionary with statistic.
Example
return dict(done=1, overwritten=0, error=0, ignored=1)
for each in file_names:
"""
ret = dict(done=0, overwritten=0, error=0, ignored=0)
# Progress output 'Processing RAW 123: 1'
index = 0
progress = progress_prepare(len(analysis), 'Processing', subdir)
for each in analysis:
index += 1
sys.stdout.write(progress['back'] + progress['formatting'].format(str(index)))
sys.stdout.flush()
source = '{0}/{1}'.format(each['path'], each['file'])
destination = '{0}/{1}/{2}'.format(dest, subdir, each['file'])
if not each['exist'] or processing_flags['force']:
try:
if processing_flags['move']:
os.rename(source, destination)
else:
shutil.copy2(source, destination)
ret['done'] += 1
if each['exist']:
ret['overwritten'] += 1
except:
print('Failed to {0} file {1}'
.format(verb_present, str(each['file'])))
ret['error'] += 1
else:
ret['ignored'] += 1
sys.stdout.write('\n')
return ret
def load_test_print(files, max_name_len, type):
"""
Prints info for the load analyse list of, e.g. for jpgs.
"""
for item in files:
if item['exist']:
status = 'o'
time_str = time_readable(item['time']) + ' -> ' + \
time_readable(item['desttime'])
else:
status = ' '
time_str = time_readable(item['time'])
print('{0} {1} {2} {3}'.format(status, type,
item['file'].ljust(max_name_len), time_str))
return
def get_max_name_length(analysis, keyname, fieldname):
"""
Returns max string lenght of all items in the dict with list of dicts.
"""
names = []
for item in analysis[keyname]:
names.append(item[fieldname])
return string_get_max_length(names)
def scan_tree(path, values):
"""
Reads directory tree search image files recursive.
Expands dict with a list for 'jpg', 'raw' and 'video'.
path: absolut path
values: dict to be expanded
Example:
values= dict(jpg=[dict(path='/img/sub' file='img01.jpg'), ...],
raw=[dict(path='/img/sub' file='img01.raf'), ...],
video=[dict(path='/img/sub' file='mov01.mp4'), ...])
"""
# print('scan_tree() dir={0}'.format(str(path)))
# dirs = [d for d in os.listdir(path) if os.path.isdir('{0}/{1}'
# .format(path, d))]
dirs = [d.name for d in os.scandir(path) if d.is_dir()]
for f in list_jpg(path):
atime = os.path.getatime('{0}/{1}'.format(path, f))
values['jpg'].append(dict(path=path, file=f, time=atime))
for f in list_raw(path):
atime = os.path.getatime('{0}/{1}'.format(path, f))
values['raw'].append(dict(path=path, file=f, time=atime))
for f in list_video(path):
atime = os.path.getatime('{0}/{1}'.format(path, f))
values['video'].append(dict(path=path, file=f, time=atime))
for d in dirs:
scan_tree('{0}/{1}'.format(path, d), values)
def string_get_max_length(list):
"""
For the given list with strings, the lenght of the max. String will be
returned.
For instance:
string_get_max_length(['A', '123'])
return 3
"""
maxlen = 0
for item in list:
if len(item) > maxlen:
maxlen = len(item)
return maxlen
def time_readable(seconds):
"""
Returns String with readable time for the given timestamp.
Example:
time_redable(1474878288.2156258)
return Mon, 26 Sep 2016 10:24:48
"""
return time.strftime("%a, %d %b %Y %H:%M:%S",
time.localtime(seconds))
def get_fow_root():
"""
The root directory of a fow must have a .fow directory.
Returns String with path of the root directory of this fow with
and ending '/'.
Or, if actual directory is not within a fow, None will be returned.
"""
actual = os.getcwd()
parts = [x for x in actual.split('/') if len(x) > 0]
# print('parts=' + str(parts))
paths = []
tpath = '/'
for i in range(0, len(parts)):
tpath = tpath + parts[i] + '/'
# print(f"get_fow_root() {str(paths)}")
paths.append(tpath)
for i in range(0, len(paths) - 1):
# print(f"get_fow_root() {paths[len(paths) - 1 - i]}")
path = paths[len(paths) - 1 - i]
files = [x for x in os.listdir(path)
if x == DIR_FOW]
if len(files) == 1:
return path
# No .fw found
return None
def is_fow_root(path):
"""
Return True, if the path is the root directory of a fow,
otherwise False.
"""
for each_file in os.listdir(path):
# print('each_file=' + each_file)
if each_file == '.fow':
return True
return False
def is_fow():
"""
If the actual directory is within a fow, True will be returned
silently. Otherwise a message wil be printed and False will be returned.
"""
# print(str(get_fow_root()))
if get_fow_root() is None:
print('Actual path is not within a fow. See "help init" how to ' +
'create a new fow here or change your actual directory ' +
'to a fow.')
return False
return True
def getAllFowDirs():
"""
Returns a dictionary with all fow sub directories. Use this to create dirs
or for backup fow.
"""
dirs = {'DIR_FOW': DIR_FOW,
'DIR_00': DIR_00,
'DIR_01': DIR_01,
'DIR_02': DIR_02}
return dirs
def normalize_args_OLD(_actual, rules):
"""
Only changes shorts to name, so it's easier to analyze
the arguments.
Example:
_actual = {
names=[None, None], shorts=['c', 't'], args=[]
}
_rules = [Path1List, ...]
pathList = [testDict, createDict, ...]
testDict = {atom=atomTestDict, obligat=trueOrFalse}
atomTestDict = {name='test', short='t', args=0_1_OR_2} etc.
returns { names=['create','test'], args=[] }
"""
print('normalizeArgs() _actual=' + str(_actual))
ret = _actual.copy()
founds = set()
# print('copy=' + str(ret))
for short in _actual['shorts']:
# print('short=' + short)
for path in rules:
# print('path=' + str(path))
for node in path:
# print('node=' + str(node))
if short == node['atom']['short']:
# print('found short ' + short)
if not node['atom']['name'] in ret['names']:
ret['names'].append(node['atom']['name'])
# Delete only one time
if short in ret['shorts']:
founds.add(short)
# ret['shorts'].remove(short)
# print('founds=' + str(founds))
# print('ret=' + str(ret))
return ret
def copy_missing_jpgs(src_dir, dest_dir, dry_run):
"""
Copies all jpgs from source directory to destination directory, if they
didn't already exist there.
src_dir is the /row directory with raws to move.
dest_dir is the target directory.
"""
# print('src=' + src_dir)
# print('dst=' + dest_dir)
srcs = get_files_as_dict(list_jpg(src_dir))
# print(str(srcs))
dests = get_files_as_dict(list_jpg(dest_dir))
# print(str(dests))
# files = [s for s in srcs if not s in dests]
files = []
for each_src in srcs:
found = False
for each_dest in dests:
if each_dest['name'] == each_src['name']:
found = True
break
if not found:
files.append(each_src['filename'])
# print(str(files))
if len(files) == 0:
print('No JPGs to copy.')
return
if dry_run:
print('JPG files to copy (missing files):')
for each_file in files:
print(str(each_file))
else:
for each_file in files:
os.system('cp ' + src_dir + '/' + each_file +
' ' + dest_dir + '/' + each_file)
def path_get_subdir(path):
"""
Returns the last segment of the path,
or "", if path has no segments.
Does not care, if last path segment is a file or a subdir.
"""
if path.rfind('/', 1) == -1:
return ""
(parent, subdir) = path.rsplit('/', 1)
return subdir
def path_get_parent(path):
"""
Returns the path without the file or last sub directory,
or "", if path if path has no segments.
Does not care, if last path segment is a file or a subdir.
"""
if path.rfind('/', 1) == -1:
return ""
(parent, subdir) = path.rsplit('/', 1)
return parent
def filename_get_suffix(filename):
"""
Returns the suffix of the give file without its optional suffix.
"""
if filename.rfind('.', 1) == -1:
return ""
(name, suffix) = filename.rsplit('.', 1)
return suffix
def filename_get_name(filename):
"""
Returns the name of the give file without its optional suffix.
"""
if filename.rfind('.', 1) == -1:
return filename
(name, suffix) = filename.rsplit('.', 1)
return name
def get_files_as_dict(files):
"""
For the given list of file names, a list of dictionaries
will be retured with fields 'file', 'name' and 'suffix'
will be returned.
Example: get_files_as_dict(['img1.jpg'] returns
[dict(filename='img1.jpg', name='img1', suffix='jpg')]
"""
# print('files=' + str(files))
ret = []
for each_file in files:
ret.append(dict(filename=each_file,
name=filename_get_name(each_file),
suffix=filename_get_suffix(each_file)))
# print(str('ret=' + str(ret)))
return ret
def filename_get_type(filename):
"""
Returns the type of a file. Supported types are:
RAW, JPG, TIF
Or, if other
"""
suffixes = ['jpg', 'JPG', 'jpeg', 'JPEG']
for s in suffixes:
if filename_get_suffix(filename) == s:
return TYPE_JPG
suffixes = ['RAW', 'raw', 'RAF', 'raf', 'cr2', 'CR2']
for s in suffixes:
if filename_get_suffix(filename) == s:
return TYPE_RAW
suffixes = ['tif', 'TIF']
for s in suffixes:
if filename_get_suffix(filename) == s:
return TYPE_TIF
return None
def list_jpg(path):
"""
Like os.listdir, a list with jpg files will be returned.
Supported suffixes: jpg, JPG.
"""
suffixes = ['jpg', 'JPG', 'jpeg', 'JPEG']
return [f for f in os.listdir(path) for s in suffixes
if filename_get_suffix(f) == s]
def list_raw(path):
"""
Like os.listdir, a list with raw files will be returned.
Supported suffixes: 'RAW', 'raw', 'RAF', 'raf', 'cr2', 'CR2'.
"""
suffixes = ['RAW', 'raw', 'RAF', 'raf', 'cr2', 'CR2']
return [f for f in os.listdir(path) for s in suffixes
if filename_get_suffix(f) == s]
def list_video(path):
"""
Like os.listdir, a list with video files will be returned.
Supported suffixes: 'MOV', 'mov', 'MP4', 'mp4'.
"""
suffixes = ['MOV', 'mov', 'MP4', 'mp4']
return [f for f in os.listdir(path) for s in suffixes
if filename_get_suffix(f) == s]
def get_exif_status(path):
"""
Returns a list (sorted by name) of dictionaries with the status incl. EXIF information about
all image files in sub folder 'path', based by the image name. Example for return:
return [dict(image='image1', final=False, jpg=True, raw=True,
location=dict(lat='54.318340N', lon='18.428409E')
title='Lonely man in the park' )]
name: Image name without suffix
final, jpg, raw: True, if a file of this image is this subfolder
title: title of the final image
path must be a subdir
"""
path_final = path + '/' + DIR_FINAL
path_jpg = path + '/' + DIR_JPG
path_raw = path + '/' + DIR_RAW
path_video = path + '/' + DIR_VIDEO
dirs = os.listdir(path)
if DIR_FINAL in dirs:
final_files = os.listdir(path_final)
else:
final_files = []
if DIR_JPG in dirs:
jpg_files = list_jpg(path_jpg)
else:
jpg_files = []
if DIR_RAW in dirs:
raw_files = list_raw(path_raw)
else:
raw_files = []
if DIR_VIDEO in dirs:
video_files = list_video(path_video)
else:
video_files = []
images = []
final_images = []
jpg_images = []
raw_images = []
video_images = []
titles = dict()
locations = dict()
descriptions = dict()
jpg_exifs = images_get_exifs('{}/{}'.format(path, DIR_JPG), jpg_files, report=True)
raw_exifs = images_get_exifs('{}/{}'.format(path, DIR_RAW), raw_files, report=True)
video_exifs = images_get_exifs('{}/{}'.format(path, DIR_VIDEO), video_files, report=True)
final_exifs = images_get_exifs('{}/{}'.format(path, DIR_FINAL), final_files, report=True)
# print('get_status2() {}'.format(str(jpg_exifs)))
# for each_file in jpg_files:
# images.append(filename_get_name(each_file))
# jpg_images.append(filename_get_name(each_file))
for each_exifs in jpg_exifs:
titles[filename_get_name(each_exifs['name'])] = each_exifs['title']
descriptions[filename_get_name(each_exifs['name'])] = each_exifs['description']
locations[filename_get_name(each_exifs['name'])] = each_exifs['gps']
images.append(filename_get_name(each_exifs['name']))
jpg_images.append(filename_get_name(each_exifs['name']))
for each_exifs in raw_exifs:
titles[filename_get_name(each_exifs['name'])] = each_exifs['title']
descriptions[filename_get_name(each_exifs['name'])] = each_exifs['description']
locations[filename_get_name(each_exifs['name'])] = each_exifs['gps']
images.append(filename_get_name(each_exifs['name']))
raw_images.append(filename_get_name(each_exifs['name']))
for each_exifs in video_exifs:
titles[filename_get_name(each_exifs['name'])] = each_exifs['title']
descriptions[filename_get_name(each_exifs['name'])] = each_exifs['description']
locations[filename_get_name(each_exifs['name'])] = each_exifs['gps']
images.append(filename_get_name(each_exifs['name']))
video_images.append(filename_get_name(each_exifs['name']))
for each_exifs in final_exifs:
titles[filename_get_name(each_exifs['name'])] = each_exifs['title']
descriptions[filename_get_name(each_exifs['name'])] = each_exifs['description']
locations[filename_get_name(each_exifs['name'])] = each_exifs['gps']
images.append(filename_get_name(each_exifs['name']))
final_images.append(filename_get_name(each_exifs['name']))
# Remove duplicate names
images = set(images)
images = list(images)
# Return list has to bo sorted by file names
images.sort()
stats = []
for each_image in images:
is_in_final = each_image in final_images
is_in_jpg = each_image in jpg_images
is_in_raw = each_image in raw_images
is_in_video = each_image in video_images
stat = dict(image=each_image, final=is_in_final,
jpg=is_in_jpg, raw=is_in_raw, video=is_in_video, title=titles[each_image],
description=descriptions[each_image],
location=locations[each_image])
stats.append(stat)
# print('get_status2() {}'.format(str(stats)))
return stats
def get_exif_status_final_only(path):
"""
Like get_exif_status, but only for images in final
Example for return:
return [dict(image='image1', final=True, jpg=True, raw=True,
location=dict(lat='54.318340N', lon='18.428409E')
title='Lonely man in the park' )]
name: Image name without suffix
final, jpg, raw: True, if a file of this image is this subfolder
title: title of the final image
path must be a subdir
"""
path_final = path + '/' + DIR_FINAL
dirs = os.listdir(path)
if DIR_FINAL in dirs:
final_files = os.listdir(path_final)
else:
final_files = []
images = []
final_images = []
titles = dict()
locations = dict()
descriptions = dict()
final_exifs = images_get_exifs('{}/{}'.format(path, DIR_FINAL), final_files, report=True)
jpg_images = [filename_get_name(f) for f in list_jpg(path + '/' + DIR_JPG)]
raw_images = [filename_get_name(f) for f in list_raw(path + '/' + DIR_RAW)]
video_images = [filename_get_name(f) for f in list_video(path + '/' + DIR_VIDEO)]
for each_exifs in final_exifs:
titles[filename_get_name(each_exifs['name'])] = each_exifs['title']
descriptions[filename_get_name(each_exifs['name'])] = each_exifs['description']
locations[filename_get_name(each_exifs['name'])] = each_exifs['gps']
images.append(filename_get_name(each_exifs['name']))
final_images.append(filename_get_name(each_exifs['name']))
# Remove duplicate names
images = set(images)
images = list(images)
# Return list has to bo sorted by file names
images.sort()
stats = []
for each_image in images:
is_in_final = each_image in final_images
is_in_jpg = each_image in jpg_images
is_in_raw = each_image in raw_images
is_in_video = each_image in video_images
stat = dict(image=each_image, final=is_in_final,
jpg=is_in_jpg, raw=is_in_raw, video=is_in_video, title=titles[each_image],
description=descriptions[each_image],
location=locations[each_image])
stats.append(stat)
# print('get_status2() {}'.format(str(stats)))
return stats
def get_short_status(path):
"""
Returns a list (sorted by name) of dictionaries with the status without EXIF information about
all image files in sub folder 'path', based by the image name. Example for return:
return [dict(image='image1', final=False, jpg=True, raw=True,
location=dict(lat='54.318340N', lon='18.428409E')
title='Lonely man in the park' )]
name: Image name without suffix
final, jpg, raw: True, if a file of this image is this subfolder
title: title of the final image
path must be a subdir
"""
path_final = path + '/' + DIR_FINAL
path_jpg = path + '/' + DIR_JPG
path_raw = path + '/' + DIR_RAW
path_video = path + '/' + DIR_VIDEO
dirs = os.listdir(path)
if DIR_FINAL in dirs:
final_files = os.listdir(path_final)
else:
final_files = []
if DIR_JPG in dirs:
jpg_files = list_jpg(path_jpg)
else:
jpg_files = []
if DIR_RAW in dirs:
raw_files = list_raw(path_raw)
else:
raw_files = []
if DIR_VIDEO in dirs:
video_files = list_video(path_video)
else:
video_files = []
images = []
final_images = []
jpg_images = []
raw_images = []
video_images = []
for each in jpg_files:
images.append(filename_get_name(each))
jpg_images.append(filename_get_name(each))
for each in raw_files:
images.append(filename_get_name(each))
raw_images.append(filename_get_name(each))
for each in video_files:
images.append(filename_get_name(each))
video_images.append(filename_get_name(each))
for each in final_files:
images.append(filename_get_name(each))
final_images.append(filename_get_name(each))
# Remove duplicate names
images = set(images)
images = list(images)
# Return list has to bo sorted by file names
images.sort()
stats = []
for each_image in images:
is_in_final = each_image in final_images
is_in_jpg = each_image in jpg_images
is_in_raw = each_image in raw_images
is_in_video = each_image in video_images
stat = dict(image=each_image, final=is_in_final,
jpg=is_in_jpg, raw=is_in_raw, video=is_in_video)
stats.append(stat)
# print('get_status2() {}'.format(str(stats)))
return stats
def image_write_gps(image_path, gpx_path):
"""
Update the gps exifs of the image by the given track file.
Returns True, if gps found in track file, otherwise false
"""
cmd = 'exiftool -geotag {0} {1} -overwrite_original'.format(gpx_path, image_path)
# print('image_write_gps() cmd={}'.format(cmd))
try:
b = subprocess.check_output(
cmd,
shell=True,
stderr=subprocess.STDOUT,
universal_newlines=False)
cmd_ret = str(b)
cmd_ret = cmd_ret[0:len(cmd_ret) - 1]
# print('image_write_gps() value={}'.format(str(cmd_ret)))
except subprocess.CalledProcessError as e:
# print(str(e))
return False
return True
def images_get_exifs(path, file_names, report=True):
"""
Returns a list of dictionaries with all supported exif information for the give files.
file_names: List with file names without a path
path: Path to the files
:param report: With True, status will be printed. Default is true (silence)
Example
return [
dict(name='img01.jpg', gps=dict(lon=1.0, lat=49,54.318340N), title='A huge tree',
description='Very short description'
createdate='2017:06:19 08:16:11',
... ]
"""
ret = []
# print('Reading images', end='')
index = 0
if report:
progress = progress_prepare(len(file_names), 'Reading', path)
for each in file_names:
index += 1
if report:
sys.stdout.write(progress['back'] + progress['formatting'].format(str(index)))
sys.stdout.flush()
cmd = 'exiftool -T -n -filename -gpslatitude -gpslongitude -title -createdate -description -author {}/{}'.format(path, each)
# print('images_get_exifs() cmd={}'.format(cmd))
try:
b = subprocess.check_output(
cmd,
shell=True,
universal_newlines=True)
cmd_ret = str(b)
cmd_ret = cmd_ret[0:len(cmd_ret) - 1]
# print('images_get_exifs() value={}'.format(str(cmd_ret)))
except subprocess.CalledProcessError as e:
ret.append(dict(name=each, title=None, gps=None, createdate=None, author=None))
# print(str(e))
continue
values = cmd_ret.split('\t')
# print('images_get_exifs() value_list={}'.format(str(values)))
# Not really nice and error prone but null values are set to '-', so we have to change this
if values[6] == '-':
author = None
else:
author = values[6]
if values[4] == '-':
createdate = None
else:
createdate = values[4]
if values[3] == '-':
title = None
else:
title = values[3]
if values[5] == '-':
description = None
else:
description = values[5]
if values[1] == '-' and values[2] == '-':
gps = None
else:
gps = dict(lat=values[1], lon=values[2])
ret.append(dict(name=values[0], title=title, description=description, gps=gps, createdate=createdate,
author=author))
# print('Done.')
if report:
sys.stdout.write('\n')
sys.stdout.flush()
return ret
def image_get_exifs(path, file_name):
"""
Returns a dictionary with all supported exif information for the give file. Non existing attributes will return
value None.
file_name: file name without a path
path: Path to the file
Example
return
dict(name='img01.jpg', gps=dict(lan=1.0, lat=49,54.318340N), title='A huge tree',
createdate='2017:06:19 08:16:11')
"""
cmd = 'exiftool -T -filename -gpslatitude -gpslongitude -title -createdate {}/{}'.format(path, file_name)
# print('images_get_exifs() cmd={}'.format(cmd))
try:
b = subprocess.check_output(
cmd,
shell=True,
universal_newlines=True)
cmd_ret = str(b)
cmd_ret = cmd_ret[0:len(cmd_ret) - 1]
# print('images_get_exifs() value={}'.format(str(cmd_ret)))
except subprocess.CalledProcessError as e:
return dict(name=file_name, title=None, gps=None, createdate=None)
# print(str(e))
values = cmd_ret.split('\t')
# print('images_get_exifs() value_list={}'.format(str(values)))
# Not really nice and error prone but null values are set to '-', so we have to change this
if values[4] == '-':
createdate = None
else:
createdate = values[4]
if values[3] == '-':
title = None
else:
title = values[3]
if values[1] == '-' and values[2] == '-':
gps = None
else:
gps = dict(lat=values[1], lon=values[2])
return dict(name=values[0], title=title, gps=gps, createdate=createdate)
def image_get_xmp_tag(filename, tagname):
"""
Reads the xmp-exif data of the given file and returns the value as string.
Returns None, if not found.
"""
try:
b = subprocess.check_output(
'exiv2 -PXt -K ' + tagname + ' ' + filename,
stderr=subprocess.STDOUT,
shell=True,
universal_newlines=True)
except subprocess.CalledProcessError:
# print('exiv2 failed for ' + filename)
return None
# print('image_get_title return=' + title)
return str(b)
def image_get_time(filename):
"""
Reads the exif date and time of the given file and returns the value as
string in format yyyymmtt-hhmmss
Returns None, if not found.
Example:
return '20161231-235959'
"""
# print('image_get_time() filename=' + filename)
try:
b = subprocess.check_output(
# 'exiv2 -Pt -g xif.Photo.DateTimeOriginal '
'exiftool -DateTimeOriginal -T {}'.format(filename),
stderr=subprocess.STDOUT,
shell=True,
universal_newlines=True)
# except subprocess.CalledProcessError as err:
except subprocess.CalledProcessError:
# print(str(err))
return None
# print('image_get_time(): ' + str(b))
parts = str(b).split(' ')
if parts is None:
return None
if len(parts) < 2:
return None
ret = parts[0] + '-' + parts[1]
ret = ret.replace(':', '')
ret = ret.replace('\n', '')
# print('image_get_time(): ' + str(b))
return ret
def video_get_time(filename):
"""
Reads the exif date and time of the given file and returns the value as
string in format yyyymmtt-hhmmss
Returns None, if not found.
Example:
return '20161231-235959'
"""
# print('image_get_time() filename=' + filename)
try:
b = subprocess.check_output(
'exiftool -T -createdate '
+ filename,
stderr=subprocess.STDOUT,
shell=True,
universal_newlines=True)
# except subprocess.CalledProcessError as err:
except subprocess.CalledProcessError:
# print(str(err))
return None
# print('image_get_time(): ' + str(b))
parts = str(b).split(' ')
if parts is None:
return None
if len(parts) < 2:
return None
ret = parts[0] + '-' + parts[1]
ret = ret.replace(':', '')
ret = ret.replace('\n', '')
return ret
def image_get_model(filename):
"""
Reads the exif model of the given file and returns the value as string
Returns None, if not found.
Example:
return 'X-E2S'