From 6a44a6143f453346f74c4f160200460adc7cbb54 Mon Sep 17 00:00:00 2001 From: Leonard Kugis Date: Tue, 11 Apr 2023 02:49:17 +0200 Subject: Implemented renaming schemes --- util.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'util.py') diff --git a/util.py b/util.py index 9fca80c..5e0ccce 100644 --- a/util.py +++ b/util.py @@ -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 -- cgit v1.2.1