-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_by_exif.py
108 lines (93 loc) · 3.24 KB
/
rename_by_exif.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
"""
Script to rename images in selected folder according to EXIF datetime_origin
depend on: 'exif' - pip3 install exif
usage: python3 rename_by_exif.py
"""
import os
import sys
import tkinter as tk
from datetime import datetime
from exif import Image
from tkinter import filedialog
def set_directory() -> str:
"""Tkinter dialogue to select directory for processing
return directory path as a string.
"""
path_find = tk.Tk()
path_find.withdraw()
path = filedialog.askdirectory()
if path:
return path
print("No directory selected!")
sys.exit()
def exif_date_time(file_path: str) -> str:
"""Retrieve the EXIF informations from file
return string in format: YYYY-mm-dd_HHMMSS
return "missing exif" if not image or EXIF is missing
"""
with open(file_path, "rb") as image_file:
my_image = Image(image_file)
try:
exD = datetime.strptime(
my_image.datetime_original,
"%Y:%m:%d %H:%M:%S"
)
exif_date_time = datetime.strftime(exD, "%Y-%m-%d_%H%M%S")
return exif_date_time
except AttributeError:
return "missing exif"
def exif_rename(folder_path: str, jpg: str, datetime: str) -> None:
"""rename one JPG file"""
src = os.path.join(folder_path, jpg) # old filename path
dst = os.path.join(folder_path, datetime + ".jpg")
os.rename(src, dst)
if __name__ == "__main__":
is_jpg = (".jpg", ".jpeg") # alowed file extensions
# runtime statistics
processed = 0
passed = 0
duplicity = 0
files_list_clear = [] # only JPG's
folder_path = set_directory() # call for folder selection dialogue
files_list = os.listdir(folder_path) # list files in directory
if len(files_list) == 0:
print("Empty directory")
else:
# clear list - only JPG files
for file in files_list:
try:
extension = file[file.rindex("."):]
if extension.lower() in is_jpg:
files_list_clear.append(file)
except ValueError:
continue
# rename files if EXIF datetime find
for jpg in files_list_clear:
date_time = exif_date_time(os.path.join(folder_path, jpg))
# skip no exif files
if date_time == "missing exif":
passed += 1
pass
# skip exif duplicity images (edited versions etc...)
elif os.path.isfile(os.path.join(folder_path, date_time + ".jpg")):
duplicity += 1
pass
# if all clear, rename file
else:
exif_rename(folder_path, jpg, date_time)
processed += 1
# final report
if processed > 0:
print(f"Images renamed: {str(processed)}")
print(f"Files skiped: {str(passed)}")
if duplicity > 0:
print(
f"There was {str(duplicity)} skiped file \n"
"EXIF match, missing or file exist."
)
else:
print(
f"No files out of {str(len(files_list))} processed.\n"
"EXIF match, missing or file exist."
)