-
Notifications
You must be signed in to change notification settings - Fork 0
/
picOrganizer.py
78 lines (61 loc) · 2.88 KB
/
picOrganizer.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
import os
import re
from PIL import Image
# TODO: add argparse.ArgumentParser to designate WD. Consider option --dry-run.
# get the year
def year_taken(path):
date_taken = Image.open(path)._getexif()[36867]
return date_taken[0:4]
# using the year data from the image, check if a folder exists.
# If not, then create it. Move the file to the folder.
def sort_file(file_name, year_photo_taken):
if not os.path.exists(year_photo_taken):
os.makedirs(year_photo_taken)
os.rename(file_name, year_photo_taken + '\\' + file_name)
print(file_name + ' moved.')
# define what image and video types to search for
IMAGE_TYPES = ('.png', '.jpeg', '.jpg', '.gif')
video_types = ('.mov', 'mp4')
# Search for 4 digit string starting with '20'
year_regex = re.compile(r'20(\d){2}')
# You can either place this script in the folder to sort, or you can manually
# specify which directory you want. To organize the script directory, comment
# out the second two lines of code in this block. To organize a specific directory,
# comment out the first line of code in this block.
file_path = os.getcwd()
# file_path = os.path.join(os.path.expanduser('~'), "Dropbox", "Camera Uploads") # os.path.expanduser('~') gets the path for the user's home folder.
# os.chdir(file_path)
def main():
# Process images
for file_name in os.listdir(file_path):
if file_name.endswith(IMAGE_TYPES): # 'endswith() can be fed a tuple.
try:
# Try to get the 'date taken' image, lack of info throws an exception.
year = year_taken(file_name)
sort_file(file_name, year)
except:
# If 'date taken' not in exif data, get the year from the file name
print('Date taken not found in exif data for ' + file_name + '.')
regex_match = year_regex.search(file_name)
if regex_match is None:
# if you can't find year in the file name, leave it alone.
print('Year not found in filename for ' + file_name + ', file not moved.')
else:
year = regex_match.group()
sort_file(file_name, year)
# Process videos
for file_name in os.listdir(file_path):
if file_name.endswith(video_types):
try:
# No exif data in videos - do a regex search for year in the file name
regex_match = year_regex.search(file_name)
if regex_match is None:
# If a year isn't found, then leave the file there to be manually sorted.
print('Year not found in filename for ' + file_name + ', file not moved.')
else:
year = regex_match.group()
sort_file(file_name, year)
except:
print('Unable to sort ' + file_name + '.')
if __name__ == "__main__":
main()