Created
December 13, 2019 23:17
-
-
Save azye/d55433cdecba5e06510923625ea0d9b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os, argparse, sys, time, shutil | |
MS_CONVERT_TIME = 1000 | |
parser = argparse.ArgumentParser(description='Rename image filenames') | |
parser.add_argument('start', type=int, help='starting ID to use as ballot ID') | |
parser.add_argument('input', help='input directory of images') | |
parser.add_argument('output', help='output directory for images') | |
parser.add_argument('-v', '--verbose', action='store_true', help='verbose printing') | |
parser.add_argument('-p', '--prefix', help='ballot ID prefix to use') | |
parser.add_argument('-w', '--wait', type=int, help='milliseconds to wait before processing next image') | |
parser.add_argument('-d', '--dry', action='store_true', help='prints the expected rename paths without executing') | |
args = parser.parse_args() | |
in_dir, out_dir, prefix, start, verbose, wait, dry = args.input, args.output, args.prefix, args.start, args.verbose, args.wait, args.dry | |
def generate_image_file_name(out_dir, prefix, serial): | |
""" Generate the output file name paths | |
""" | |
if prefix: | |
return [os.path.join(out_dir, 'Valid_test_' + prefix + ('0' * (10 - len(prefix) - len(str(serial)))) + str(serial) + 'A.jpg'), os.path.join(out_dir, 'Valid_test_' + prefix + ('0' * (10 - len(prefix) - len(str(serial)))) + str(serial) + 'B.jpg')] | |
return [os.path.join(out_dir, 'Valid_test_' + ('0' * (10 - len(str(serial)))) + str(serial) + 'A.jpg'), os.path.join(out_dir, 'Valid_test_' + ('0' * (10 - len(str(serial)))) + str(serial) + 'B.jpg')] | |
def recreate_AB_image_file_names(currentpath, id): | |
""" Recreates the image pair file names for both A and B sides | |
""" | |
return [os.path.join(currentpath, 'Valid_test_'+id+'A.jpg'), os.path.join(currentpath, 'Valid_test_'+id+'B.jpg')] | |
def remove_prefix(text, prefix): | |
""" Removes a prefix from a string | |
""" | |
if text.startswith(prefix): | |
return text[len(prefix)+1:] | |
return text | |
visited = set() | |
serial = start | |
start_time = int(round(time.time() * MS_CONVERT_TIME)) | |
total_files = 0 | |
# counts the file names in the directory before copying | |
for currentpath, folders, files in os.walk(in_dir): | |
for file in files: | |
if file[0] != '.': | |
total_files += 1 | |
# walks in_dir for all files (including subdirs) and copies files with inputted arguments | |
for currentpath, folders, files in os.walk(in_dir): | |
for file in files: | |
ballotID = file[len(file)-15:len(file)-5] | |
# skip dot files or ballotIDs that we've already seen | |
if ballotID in visited or file[0] == '.': | |
continue | |
# check the elapsed time to control copy flow | |
elapsed_ms = int(round(time.time() * MS_CONVERT_TIME)) - start_time | |
if elapsed_ms < wait and serial != 1: | |
time.sleep((wait - elapsed_ms) / MS_CONVERT_TIME) | |
start_time = int(round(time.time() * MS_CONVERT_TIME)) | |
# gets input and output names | |
image_pairs = recreate_AB_image_file_names(currentpath, ballotID) | |
rename_pairs = generate_image_file_name(os.path.join(out_dir, remove_prefix(currentpath, in_dir)), prefix, serial) | |
print str(serial) + '/' + str(total_files / 2) + ' copying ' + image_pairs[0] + ', ' + image_pairs[1] + ' to ' + rename_pairs[0] + ', ' + rename_pairs[1] | |
# if not a dry run, copy file pairs | |
if not dry: | |
dstfolder = os.path.dirname(rename_pairs[0]) | |
if not os.path.exists(dstfolder): | |
os.makedirs(dstfolder) | |
shutil.copyfile(image_pairs[0], rename_pairs[0]) | |
shutil.copyfile(image_pairs[1], rename_pairs[1]) | |
serial += 1 | |
visited.add(ballotID) | |
# touch Valid_test_1351115729A.jpg | |
# touch Valid_test_1351115729B.jpg | |
# touch Valid_test_1351968155A.jpg | |
# touch Valid_test_1351968155B.jpg | |
# touch Valid_test_1351975885A.jpg | |
# touch Valid_test_1351975885B.jpg | |
# touch Valid_test_1351975886A.jpg | |
# touch Valid_test_1351975886B.jpg | |
# touch Valid_test_1351975887A.jpg | |
# touch Valid_test_1351975887B.jpg | |
# touch a12b/Valid_test_1351115729A.jpg | |
# touch a12b/Valid_test_1351115729B.jpg | |
# touch a12b/Valid_test_1351968155A.jpg | |
# touch a12b/Valid_test_1351968155B.jpg | |
# touch a12b/Valid_test_1351975885A.jpg | |
# touch a12b/Valid_test_1351975885B.jpg | |
# touch a12b/Valid_test_1351975886A.jpg | |
# touch a12b/Valid_test_1351975886B.jpg | |
# touch a12b/Valid_test_1351975887A.jpg | |
# touch a12b/Valid_test_1351975887B.jpg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment