2
« on: July 22, 2018, 05:01:36 PM »
This script allows you to rename the images so that PhotoScan treats them in the right order:
DISCLAIMER: I'm not responsible for any damage or problems this script might cause. Use it responsibly.
import os
import sys
import argparse
def load_bundler_list(bundler_list_path, fileformat, reverse):
lines = {}
with open(bundler_list_path, "r") as file:
for idx, line in enumerate(file):
if line == "":
break
if reverse:
lines[str(idx) + fileformat] = line.strip();
else:
lines[line.strip()] = str(idx) + fileformat;
return lines
def load_image_names(img_source_path):
images = []
for root, dirs, files in os.walk(img_source_path, topdown=True):
images.extend(files)
break
return images
def rename_file(path, old_name, new_name):
src = os.path.join(path, old_name)
dst = os.path.join(path, new_name)
os.rename(src, dst)
print("old name: {}, new name: {}".format(src, dst))
def rename_file_to_match_bundler(img_source_path, bundler_list_path, filename_prefix, fileformat, reverse):
images = load_image_names(img_source_path)
bundler = load_bundler_list(bundler_list_path, fileformat, reverse)
for image in images:
if image.startswith(filename_prefix) and image.endswith(fileformat):
old_name = image
new_name = bundler[image]
rename_file(img_source_path, old_name, new_name)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Image renamer - Renames image to reflect order in *.out.list.txt when sorted alphanumerically")
parser.add_argument("--source", type=str, help="Path that will be searched for images.", required=True)
parser.add_argument("--bundler", type=str, help="Path to bundler *.out.list.txt", required=True)
parser.add_argument("--prefix", type=str, help="Filename needs to start with this string, otherwise the file is ignored.", required=True)
parser.add_argument("--format", type=str, help="Image format, file ending.", required=True)
parser.add_argument('--reverse', help="Restore old name.", dest='reverse', action='store_true')
parser.set_defaults(reverse=False)
args = parser.parse_args()
rename_file_to_match_bundler(args.source, args.bundler, args.prefix, args.format, args.reverse)