-
Notifications
You must be signed in to change notification settings - Fork 0
/
photoOrganizer.py
43 lines (35 loc) · 1.29 KB
/
photoOrganizer.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
import os
import shutil
import datetime
from PIL import Image
class PhotoOrganizer:
extensions = ['jpg','jpeg','png','JPG','JPEG','PNG']
def getFolderPathFromPhotoDate(self, file):
date = self.getPhotoShootingDate(file)
return date.strftime('%Y') + '/' + date.strftime('%Y-%m-%d')
def getPhotoShootingDate(self, file):
photo = Image.open(file)
info = photo._getexif()
if (info == None):
date = datetime.datetime.fromtimestamp(os.path.getmtime(file))
else:
if 36867 in info:
date = info[36867]
date = datetime.datetime.strptime(date, '%Y:%m:%d %H:%M:%S')
else:
date = datetime.datetime.fromtimestamp(os.path.getmtime(file))
return date
def movePhoto(self, file):
new_folder = self.getFolderPathFromPhotoDate(file)
if not os.path.exists(new_folder):
os.makedirs(new_folder)
print(new_folder + '/' + file)
shutil.move(file, new_folder + '/' + file)
def organize(self):
photos = [
filename for filename in os.listdir('.') if any(filename.endswith(ext) for ext in self.extensions)
]
for filename in photos:
self.movePhoto(filename)
PO = PhotoOrganizer()
PO.organize()