From 6b0b15bde7b061ac2851c986070c6e61271782f2 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Sun, 19 Mar 2023 00:31:48 -0700 Subject: use pyexiftool wrapper instead of exiftool exec directly --- exif.py | 81 ++++++++++++++++--------------------------------------- requirements.txt | Bin 295 -> 666 bytes 2 files changed, 23 insertions(+), 58 deletions(-) diff --git a/exif.py b/exif.py index 233ceb5..f526683 100644 --- a/exif.py +++ b/exif.py @@ -1,69 +1,34 @@ import os import pathlib -import subprocess +import platform import sys +import exiftool -def rename(month): - base_directory = pathlib.Path("/Users/clarkzjw/Desktop/") - directory = str(base_directory.joinpath(month).absolute()) + "/" +def exif_rename(dir: str): + base_directory = pathlib.Path("C:\\Users\\clarkzjw\\Desktop") + directory = str(base_directory.joinpath(dir).absolute()) + "\\" files = os.listdir(directory) for file in files: - if file.startswith("IMG_") and (file.endswith(".CR2") or file.endswith(".CR3")): - filename = directory + file + filename = directory + file + ext = file.split(".")[-1] + if platform.system() == "Windows": + old_filename = filename.split("\\")[-1].split(".")[0] + else: old_filename = filename.split("/")[-1].split(".")[0] - img = filename - - if file.endswith(".CR3"): - result = subprocess.run(["bash", "-c", "exiftool {} | grep '^Time Stamp'".format(img)], - stdout=subprocess.PIPE) - timestamp = result.stdout.decode('utf-8').strip("\n").replace(".", "-").strip("Time Stamp")[2:] \ - .replace(":", "-").strip("\n") - filename = directory + timestamp + "-{}.CR3".format(old_filename) - - elif file.endswith(".CR2"): - result = subprocess.run(["bash", "-c", "exiftool {} | grep '^Date/Time Original'".format(img)], - stdout=subprocess.PIPE) - timestamp = result.stdout.decode('utf-8').split('\n')[1].replace(".", "-").strip("Date/Time Original")[ - 2:].replace(":", "-"). \ - strip("\n") - filename = directory + timestamp + "-{}.CR2".format(old_filename) - print(filename) - os.rename(img, filename) - - elif file.startswith("PXL_") and file.endswith(".dng"): - filename = directory + file - old_filename = filename.split("/")[-1].split(".")[0] - img = filename - result = subprocess.run(["bash", "-c", "exiftool {} | grep '^Date/Time Original'".format(img)], - stdout=subprocess.PIPE) - timestamp = \ - result.stdout.decode('utf-8').strip("\n").split('\n')[1].strip("Date/Time Original")[2:].split('-')[ - 0][:-4].replace(".", "-").replace(":", "-").strip("\n") - filename = directory + timestamp + "-{}.dng".format(old_filename) - print(filename) - os.rename(img, filename) - - elif file.startswith("R") and file.endswith(".DNG"): - filename = directory + file - old_filename = filename.split("/")[-1].split(".")[0] - img = filename - result = subprocess.run(["bash", "-c", "exiftool {} | grep '^Date/Time Original'".format(img)], - stdout=subprocess.PIPE) - timestamp = result.stdout.decode('utf-8').strip("\n").strip("Date/Time Original")[2:].replace(":", "-") - filename = directory + timestamp + "-{}.DNG".format(old_filename) - print(filename) - os.rename(img, filename) - - elif file.startswith("R") and file.endswith(".JPG"): - filename = directory + file - old_filename = filename.split("/")[-1].split(".")[0] - img = filename - result = subprocess.run(["bash", "-c", "exiftool {} | grep '^Date/Time Original'".format(img)], - stdout=subprocess.PIPE) - timestamp = result.stdout.decode('utf-8').strip("\n").strip("Date/Time Original")[2:].replace(":", "-") - filename = directory + timestamp + "-{}.JPG".format(old_filename) + img = filename + + if (file.startswith("IMG_") and (file.endswith(".CR2") or file.endswith(".CR3"))) or \ + (file.startswith("R") and (file.endswith(".JPG") or file.endswith(".DNG"))) or \ + (file.startswith("PXL_") and file.endswith(".dng")) or \ + (file.startswith("DSC") and file.endswith(".ARW")): + with exiftool.ExifToolHelper() as et: + for d in et.get_tags(filename, tags="EXIF:DateTimeOriginal"): + timestamp = d["EXIF:DateTimeOriginal"].replace(":", "-") + break + + filename = directory + timestamp + "-{}.{}".format(old_filename, ext) print(filename) os.rename(img, filename) @@ -71,4 +36,4 @@ def rename(month): if __name__ == "__main__": if len(sys.argv) <= 1: exit(0) - rename(sys.argv[1]) + exif_rename(sys.argv[1]) diff --git a/requirements.txt b/requirements.txt index 0febcdd..141dec7 100644 Binary files a/requirements.txt and b/requirements.txt differ -- cgit v1.2.3