aboutsummaryrefslogtreecommitdiff
blob: 7ae3cc58fbce541ff6ad88f0f9b84e85acbad41b (plain) (blame)
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
import os
import pathlib
import subprocess
import sys


def rename(month):
    base_directory = pathlib.Path("/mnt/c/Users/clarkzjw/Desktop/")
    directory = str(base_directory.joinpath(month).absolute()) + "/"

    files = os.listdir(directory)
    for file in files:
        if file.startswith("IMG_") and (file.endswith(".CR2") or file.endswith(".CR3")):
            filename = directory + file
            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)


if __name__ == "__main__":
    if len(sys.argv) <= 1:
        exit(0)
    rename(sys.argv[1])
Powered by cgit v1.2.3 (git 2.41.0)