-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_sort.py
46 lines (37 loc) · 1.17 KB
/
image_sort.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 7 19:09:41 2020
@author: palash
"""
import shutil, os, re
from PIL import Image
import datetime
from datetime import datetime
parent_dir = "/home/palash/" # for my system
photo_dir = '/home/palash/Pictures/' # OR os.getcwd()
# parent_dir = photo_dir[photo_dir.rfind('/')] ## FOR WINDOWS
photos, a = [], []
# parent_dir += "\\" ## For Windows
# photo_dir += "\\" ## For Windows
for photo in os.walk(photo_dir):
photos += [photo]
for i in photos:
for j in i:
if type(j) == list and not j == []:
a += j
### Assuming all photos are in the same directory ###
for images in a:
print(images)
match1 = re.search( r'\d{4}-\d{2}-\d{2}', images)
print(match1)
match2 = re.search( r'\d{8}', images)
if match1:
date = str(datetime.strptime(match1.group(), '%Y-%m-%d').date())
if match2:
date = str(datetime.strptime(match2.group(), '%Y%m%d').date())
if os.path.isdir(parent_dir + date):
shutil.move(photo_dir + images, parent_dir + date)
else:
os.mkdir(parent_dir + date)
shutil.move(photo_dir + images, parent_dir + date)