Created
August 30, 2014 13:54
-
-
Save tkhduracell/a28c2a3937ac9e9093ba to your computer and use it in GitHub Desktop.
Rename all files to yyyy-mm-dd hh.mm.ss format, using EXIF if possible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import hashlib | |
import os | |
import fnmatch | |
import json | |
import time | |
import datetime | |
import my_utils | |
import urllib | |
import urllib.parse | |
import base64 | |
import pystrapper | |
import exifread | |
import warnings | |
import re | |
from PIL import Image | |
from PIL.ExifTags import TAGS | |
#sys.argv[1] | |
def main(): | |
# Target directory argument | |
if len(sys.argv) - 1 < 1: | |
print("You must provide a target path as first argument") | |
sys.exit(1) | |
target_dir = os.path.abspath(sys.argv[1]) | |
if not os.path.isdir(target_dir): | |
print("Target must be a directory") | |
sys.exit(1) | |
# -rename argument | |
do_rename = False | |
if len(sys.argv) - 1 >= 2: | |
do_rename = (sys.argv[2] == "-rename") | |
print("Target directory: {}".format(target_dir)) | |
print("Output format: %Y-%m-%d %H.%M.%S") | |
print("Iterating files") | |
date_format = "%Y-%m-%d %H.%M.%S" | |
date_regex = re.compile(r"\d{4}-\d{2}-\d{2} \d{2}.\d{2}.\d{2}\.\w{1,4}") | |
for f in get_files(target_dir): | |
basename = os.path.basename(f) | |
if date_regex.search(basename): | |
print(" {} is already on correct format".format(basename)) | |
continue | |
ext = os.path.splitext(basename)[1] | |
new_filename = get_date(f).strftime(date_format) + ext | |
new_abspath = os.path.join(target_dir, new_filename) | |
print(" {} \n=> {}".format(f, new_abspath)) | |
if do_rename: | |
os.rename(f, new_abspath) | |
def get_files(target_dir, filter="*.*"): | |
for root, dirnames, filenames in os.walk(target_dir): | |
for filename in fnmatch.filter(filenames, filter): | |
yield os.path.join(root, filename) | |
@my_utils.cached | |
def hash_for_file(f, block_size=2**20): | |
md5 = hashlib.md5() | |
with open(f, mode='rb') as fs: | |
while True: | |
data = fs.read(block_size) | |
if not data: | |
break | |
md5.update(data) | |
return md5.hexdigest() | |
def intTry(value): | |
try: | |
return int(value) | |
except ValueError: | |
return -1 | |
def get_date(path_name): | |
exif = get_exif(path_name) | |
date_format = "%Y:%m:%d %H:%M:%S" | |
if "DateTimeOriginal" in exif and "DateTimeDigitized" in exif: | |
if exif["DateTimeDigitized"] == exif["DateTimeOriginal"]: | |
try: | |
dt = exif["DateTimeDigitized"] | |
return datetime.datetime.strptime(dt, date_format) | |
except: | |
pass | |
else: | |
dts = datetime.datetime.strptime(exif["DateTimeDigitized"], date_format) | |
dto = datetime.datetime.strptime(exif["DateTimeOriginal"], date_format) | |
delta = dts - dto | |
if delta.seconds == 3600*2 or delta.seconds == 3600*1: | |
return max(dts,dto) | |
else: | |
print("Missmatch in Digitized/Original: (selected earliest date)") | |
print(repr(exif["DateTimeDigitized"])) | |
print(repr(exif["DateTimeOriginal"])) | |
return min(dts,dto) | |
if "DateTime" in exif: | |
try: | |
dt = exif["DateTime"] | |
return datetime.datetime.strptime(dt, date_format) | |
except: | |
pass | |
return datetime.datetime.fromtimestamp(os.path.getmtime(path_name)) | |
def get_exif(path_name): | |
exif = {} | |
with warnings.catch_warnings(): | |
warnings.simplefilter("ignore") | |
try: | |
im = Image.open(path_name) | |
info = im._getexif() | |
if info is not None: | |
for tag in [tag for tag in info.keys() if tag in TAGS and not TAGS[tag] in ['MakerNote','UserComment']]: | |
exif[TAGS[tag]] = info[tag] | |
except: | |
pass | |
return exif | |
def print_date(exif): | |
if "DateTime" in exif: | |
print("DateTime: "+exif["DateTime"]) | |
if "DateTimeDigitized" in exif: | |
print("DateTimeDigitized: "+exif["DateTimeDigitized"]) | |
if "DateTimeOriginal" in exif: | |
print("DateTimeOriginal: "+exif["DateTimeOriginal"]) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment