diff options
author | Leonard Kugis <leonard@kug.is> | 2023-04-11 02:49:17 +0200 |
---|---|---|
committer | Leonard Kugis <leonard@kug.is> | 2023-04-11 02:49:17 +0200 |
commit | 6a44a6143f453346f74c4f160200460adc7cbb54 (patch) | |
tree | 17643955566033de7de28eb1f04184e48963ae02 | |
parent | 6b5a426e1750a57d5b291fa6e13ef5f36c9a44ad (diff) |
Implemented renaming schemes
-rw-r--r-- | file-tagger.py | 17 | ||||
-rw-r--r-- | util.py | 31 |
2 files changed, 48 insertions, 0 deletions
diff --git a/file-tagger.py b/file-tagger.py index d7d572b..4d25e90 100644 --- a/file-tagger.py +++ b/file-tagger.py @@ -48,6 +48,20 @@ def walk(tmsu, args): not_empty = bool(tags) logger.info("Existing tags: {}".format(tags)) + if ".tmsu" in file_path: + logger.info("Database meta file, skipping.") + continue + + logger.info("Renaming file {}".format(file_path)) + file_path = files[i] = { + "none": lambda x: x, + "sha1": rename_sha1, + "sha256": rename_sha256, + "cdate": rename_cdate, + "mdate": rename_mdate + }.get(args["rename"])(file_path) + logger.info("New file name: {}".format(file_path)) + if (not_empty and args["skip_tagged"]): logger.info("Already tagged, skipping.") continue @@ -158,6 +172,7 @@ def walk(tmsu, args): if ((not args["gui_tag"]) and (not args["skip_prompt"])): tags = set(input_with_prefill("\nTags for file {}:\n".format(file_path), ','.join(tags)).split(",")) + tags = { tag.lower().replace(" ", "_") for tag in tags } logger.info("Tagging {}".format(tags)) tmsu.tag(file_path, tags, untag=not_empty) @@ -168,6 +183,7 @@ if __name__ == "__main__": parser.add_argument('-f', '--file-dir', nargs='?', default='.', type=dir_path, help='File directory for walking (default: %(default)s)') parser.add_argument('-g', '--gui', nargs='?', const=1, default=False, type=bool, help='Show main GUI (default: %(default)s)') parser.add_argument('--tmsu-command', nargs='?', const=1, default="tmsu", type=str, help='TMSU command override (default: %(default)s)') + parser.add_argument('-r', '--rename', nargs='?', const=1, choices=["none", "sha1", "sha256", "cdate", "mdate"], default="none", type=str.lower, help='Rename files based on given scheme (default: %(default)s)') parser.add_argument('--tag-metadata', nargs='?', const=1, default=True, type=bool, help='Use metadata as default tags (default: %(default)s)') parser.add_argument('--predict-images', nargs='?', const=1, default=False, type=bool, help='Use prediction for image tagging (default: %(default)s)') parser.add_argument('--predict-images-backend', nargs='?', const=1, choices=["torch", "tensorflow", "keras"], default="torch", type=str.lower, help='Determines which backend should be used for keyword prediction (default: %(default)s)') @@ -200,6 +216,7 @@ if __name__ == "__main__": "file_dir": args.file_dir, "gui": args.gui, "tmsu_command": args.tmsu_command, + "rename": args.rename, "tag_metadata": args.tag_metadata, "predict_images": args.predict_images, "predict_images_backend": args.predict_images_backend, @@ -7,6 +7,37 @@ import os import numpy as np from queue import Queue from threading import Thread, Lock +import hashlib +import datetime + +BUF_SIZE = 65535 + +def rename(old, new_base): + ext = os.path.splitext(os.path.basename(old))[1] + new_fpath = os.path.join(os.path.dirname(old), new_base + ext) + os.rename(old, new_fpath) + return new_fpath + +def rename_hash(fpath, hash): + with open(fpath, 'rb') as f: + while True: + data = f.read(BUF_SIZE) + if not data: + break + hash.update(data) + return rename(fpath, hash.hexdigest()) + +def rename_sha1(fpath): + return rename_hash(fpath, hashlib.sha1()) + +def rename_sha256(fpath): + return rename_hash(fpath, hashlib.sha256()) + +def rename_cdate(fpath): + return rename(fpath, datetime.datetime.fromtimestamp(os.path.getctime(fpath)).strftime("%Y-%m-%d_%H:%M:%S:%f")) + +def rename_mdate(fpath): + return rename(fpath, datetime.datetime.fromtimestamp(os.path.getmtime(fpath)).strftime("%Y-%m-%d_%H:%M:%S:%f")) def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA): # initialize the dimensions of the image to be resized and |