-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageOrganizer.py
More file actions
74 lines (59 loc) · 2.83 KB
/
Copy pathImageOrganizer.py
File metadata and controls
74 lines (59 loc) · 2.83 KB
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
import os
from datetime import datetime
from PIL import Image, UnidentifiedImageError
import shutil
def get_date_taken(file_path):
try:
with Image.open(file_path) as img:
info = img._getexif()
if 36867 in info: # Check if "Date Taken" field is present
date_taken = info[36867]
return datetime.strptime(date_taken, "%Y:%m:%d %H:%M:%S")
except (AttributeError, KeyError, IndexError, ValueError, UnidentifiedImageError):
pass
return None
def organize_photos(source_folder, destination_folder):
# Iterate through all files in the source folder
for filename in os.listdir(source_folder):
source_path = os.path.join(source_folder, filename)
# Get the date taken of the file
date_taken = get_date_taken(source_path)
if date_taken:
# Create year and month folders in the destination folder
year_folder = os.path.join(destination_folder, str(date_taken.year))
month_folder = os.path.join(year_folder, f'{date_taken.month:02d}')
os.makedirs(month_folder, exist_ok=True)
# Copy the file to the appropriate month folder
destination_path = os.path.join(month_folder, filename)
# Check if the file already exists in the destination folder
# if not os.path.exists(destination_path):
# # Copy the file to the appropriate month folder
# shutil.copy(source_path, destination_path)
# else:
# print(f"File {filename} already exists in the destination folder. Skipping.")
# Check if the file already exists in the destination folder
counter = 1
while os.path.exists(destination_path):
# If file exists, append a counter to the filename
new_filename = f"{os.path.splitext(filename)[0]}_{counter}{os.path.splitext(filename)[1]}"
destination_path = os.path.join(month_folder, new_filename)
counter += 1
shutil.move(source_path, destination_path)
print("moved file: " + source_path + " to destination: " + destination_path)
# Move the file to the appropriate month folder
#destination_path = os.path.join(month_folder, filename)
#shutil.move(source_path, destination_path)
if __name__ == "__main__":
source_folder = r"C:\Users\dobne\Pictures\ToOrganize"
destination_folder = r"C:\Users\dobne\Pictures\Photos"
organize_photos(source_folder, destination_folder)
# Insert folder path in runtime instead:
# def main():
# source_folder = input("Enter the path to the source folder: ")
#
# destination_folder = input("Enter the path to the destination folder: ")
#
# organize_photos(source_folder, destination_folder)
#
# if __name__ == "__main__":
# main()