Skip to content

Instantly share code, notes, and snippets.

@BharatKalluri
Last active March 22, 2022 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BharatKalluri/78f6634ba03ca4618df69e5d2176dd07 to your computer and use it in GitHub Desktop.
Save BharatKalluri/78f6634ba03ca4618df69e5d2176dd07 to your computer and use it in GitHub Desktop.
Renames files with their hash for picture management
#!/usr/bin/env python3
import hashlib
import logging
import os.path
import shutil
import sys
from pathlib import Path
from typing import List
def get_img_file_list(dir_path: Path) -> List[Path]:
return [
f
for f in list(Path(dir_path).rglob("*"))
if not str(f).endswith(".json") and Path.is_file(f)
]
def get_hash_for_path(path: Path):
return hashlib.sha256(path.read_bytes()).hexdigest()
def move_hashed_images(src_folder_path: Path, dst_folder_path: Path):
for file_path in get_img_file_list(src_folder_path):
file_name, file_ext = os.path.splitext(file_path)
sha_256_sum = get_hash_for_path(file_path)
target_file_path = os.path.join(
Path(dst_folder_path), f"{sha_256_sum}{file_ext.lower()}"
)
if Path(target_file_path).exists():
logging.warning(f"{target_file_path} already exists, skipping")
continue
shutil.copy2(file_path, target_file_path)
if __name__ == "__main__":
src = Path(sys.argv[1])
dst = Path(sys.argv[2])
if not Path(src).is_dir() or not Path(dst).is_dir():
raise Exception("src and dst should be directories")
move_hashed_images(src, dst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment