| #! /usr/bin/env python3 | |
| # Image dataset pre processor with name normalization | |
| # by Jiri Podivin and Miroslav Jaros 2023 | |
| # v1.0 | |
| import os | |
| import glob | |
| import hashlib | |
| import argparse | |
| import re | |
| import shutil | |
| def main(source_path, dest_path): | |
| img_paths = [ | |
| e for e in glob.glob(os.path.join(source_path, '**')) | |
| if re.match(".*\.jpe?g", e, flags=re.IGNORECASE)] | |
| for image in img_paths: | |
| print(image) | |
| path_hash = hashlib.sha1(image.encode()).hexdigest() | |
| name_hash = hashlib.sha1(os.path.basename(image.encode())).hexdigest() | |
| new_name = f"{path_hash[:10]}_{name_hash[:10]}.jpg" | |
| shutil.copy(image, os.path.join(dest_path, new_name)) | |
| print(new_name) | |
| if __name__ == '__main__': | |
| parser = argparse.ArgumentParser("imageprocessor") | |
| parser.add_argument("source_path", type=str) | |
| parser.add_argument("dest_path", type=str) | |
| args = parser.parse_args() | |
| main(args.source_path, args.dest_path) | |