aboutsummaryrefslogtreecommitdiffstats
path: root/util.py
diff options
context:
space:
mode:
authorLeonard Kugis <leonard@kug.is>2025-09-17 17:52:46 +0200
committerLeonard Kugis <leonard@kug.is>2025-09-17 17:52:46 +0200
commitb47c5907ee9b818562a743b9b0615a6b8a84a280 (patch)
tree1c09f1f71b8d8d5b71c82e5afaa093b23b635801 /util.py
parent892396e3d1ed0352400663f91a190ee65da4c12a (diff)
parent38c94098f416d48b9006d7e7f8556c8543a19513 (diff)
downloadfile-tagger-b47c5907ee9b818562a743b9b0615a6b8a84a280.tar.gz
Merge remote-tracking branch 'refs/remotes/origin/master'HEADmaster
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..243d78e 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