forked from matthewrenze/rename-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rename.py
82 lines (57 loc) · 2.16 KB
/
Rename.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
# Rename Images with Date Photo Taken
# Purpose: Renames image files in a folder based on date photo taken from EXIF metadata
# Author: Matthew Renze
# Usage: python.exe Rename.py input-folder
# - input-folder = (optional) the directory containing the image files to be renamed
# Examples: python.exe Rename.py C:\Photos
# python.exe Rename.py
# Behavior:
# - Given a photo named "Photo Apr 01, 5 54 17 PM.jpg"
# - with EXIF date taken of "4/1/2018 5:54:17 PM"
# - when you run this script on its parent folder
# - then it will be renamed "20180401-175417.jpg"
# Notes:
# - For safety, please make a backup before running this script
# - Currently only designed to work with .jpg, .jpeg, and .png files
# - EXIF metadata must exist or an error will occur
# - If you omit the input folder, then the current working directory will be used instead.
# Import libraries
import os
import sys
from PIL import Image
# Set list of valid file extensions
valid_extensions = [".jpg", ".jpeg", ".png"]
# If folder path argument exists then use it
# Else use the current running folder
if len(sys.argv) < 1:
folder_path = input_file_path = sys.argv[1]
else:
folder_path = os.getcwd()
# Get all files from folder
file_names = os.listdir(folder_path)
# For each file
for file_name in file_names:
# Get the file extension
file_ext = os.path.splitext(file_name)[1]
# If the file does not have a valid file extension
# then skip it
if (file_ext not in valid_extensions):
continue
# Create the old file path
old_file_path = os.path.join(folder_path, file_name)
# Open the image
image = Image.open(old_file_path)
# Get the date taken from EXIF metadata
date_taken = image._getexif()[36867]
# Close the image
image.close()
# Reformat the date taken to "YYYYMMDD-HHmmss"
date_time = date_taken \
.replace(":", "") \
.replace(" ", "-")
# Combine the new file name and file extension
new_file_name = date_time + file_ext
# Create the new folder path
new_file_path = os.path.join(folder_path, new_file_name)
# Rename the file
os.rename(old_file_path, new_file_path)