aboutsummaryrefslogtreecommitdiff
path: root/util.py
diff options
context:
space:
mode:
authorLeonard Kugis <leonard@kug.is>2023-04-11 02:49:17 +0200
committerLeonard Kugis <leonard@kug.is>2023-04-11 02:49:17 +0200
commit6a44a6143f453346f74c4f160200460adc7cbb54 (patch)
tree17643955566033de7de28eb1f04184e48963ae02 /util.py
parent6b5a426e1750a57d5b291fa6e13ef5f36c9a44ad (diff)
Implemented renaming schemes
Diffstat (limited to 'util.py')
-rw-r--r--util.py31
1 files changed, 31 insertions, 0 deletions
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