-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort-photos
executable file
·42 lines (39 loc) · 1.44 KB
/
sort-photos
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
#!/usr/bin/env python3
#
# Created on 2021-12-09 to sort photos based on exif date time original.
# Photos are sorted from argv[1] into argv[2]/yyyy/mm/dd/
#
# Example use:
# ./sort-photos ~/Pictures/originals/ ~/Dropbox/Photos/
#
import os
import shlex
import subprocess
import sys
# To delete hidden files:
# find originals/ -name ".*" -type f -delete
# identify -format "%[EXIF:DateTimeOriginal]"
def sort(src, dst):
for root, dirs, files in os.walk(src):
for file in files:
base, ext = os.path.splitext(file)
path = os.path.join(root, file)
# print(path)
if '.jpeg' == ext:
# print(ext)
cmdpath = shlex.quote(path)
cmd = f'identify -format "%[EXIF:DateTimeOriginal]" {cmdpath}'
# print(cmd)
date = subprocess.check_output(cmd, shell=True, text=True)
if not date or len(date) < 10:
print(f'Error Date: {path} (date)')
continue
datepath = date[0:10].replace(':', '/')
prefix = date.replace(':', '-').replace(' ', '_')
dir = os.path.join(dst, datepath)
dstpath = os.path.join(dir, f'{prefix}-{file}')
print(f'{path} -> {dstpath}')
os.makedirs(dir, exist_ok=True)
os.rename(path, dstpath)
if (sys.argv[1] and sys.argv[2]):
sort(sys.argv[1], sys.argv[2])