file-setup/setup_files

61 lines
1.7 KiB
Plaintext
Raw Normal View History

2020-01-07 14:13:10 +00:00
#!/usr/bin/env python3.5
# coding=utf-8
"""
merge_images
Usage: For usage instructions run with option --help
Author: Stephan Porada <sporada@uni-bielefeld.de>
"""
import argparse
2020-01-27 14:52:20 +00:00
import os
import subprocess
2020-01-07 14:13:10 +00:00
def parse_arguments():
parser = argparse.ArgumentParser(description='Merges given input images '
'into one multipage tiff.')
2020-02-17 13:48:49 +00:00
parser.add_argument('-i',
dest='input_dir',
required=True,
2020-01-27 14:52:20 +00:00
type=os.path.abspath)
2020-02-17 13:48:49 +00:00
parser.add_argument('-o',
dest='output_dir',
required=True,
2020-01-27 14:52:20 +00:00
type=os.path.abspath)
2020-02-17 13:48:49 +00:00
parser.add_argument('--zip',
default='combines-pictures',
dest='zip',
type=str,
help='package result files in zip bundles',
2020-02-03 14:07:41 +00:00
required=False)
2020-01-07 14:13:10 +00:00
return parser.parse_args()
2020-02-03 14:07:41 +00:00
def merge_images(input_dir, output_dir, zip):
2020-01-27 14:52:20 +00:00
try:
os.mkdir(output_dir)
except FileExistsError:
pass
cmd = 'ls -Q -v {i}/*.* > {i}/file_list.txt'.format(i=input_dir)
subprocess.run(cmd, shell=True)
2020-02-03 14:07:41 +00:00
cmd = 'convert @{i}/file_list.txt {o}/combined.tif'.format(i=input_dir, o=output_dir)
2020-01-27 14:52:20 +00:00
subprocess.run(cmd, shell=True)
cmd = 'rm {i}/file_list.txt'.format(i=input_dir)
2020-01-28 10:06:11 +00:00
subprocess.run(cmd, shell=True)
2020-02-03 14:07:41 +00:00
if zip:
2020-02-17 13:48:49 +00:00
cmd = 'cd {o} && zip -m {f}-combined.zip combined.tif && cd -'.format(o=output_dir, f=zip)
2020-02-03 14:07:41 +00:00
subprocess.run(cmd, shell=True)
2020-01-07 14:13:10 +00:00
def main():
args = parse_arguments()
2020-02-03 14:07:41 +00:00
merge_images(args.input_dir, args.output_dir, args.zip)
2020-01-07 14:13:10 +00:00
if __name__ == '__main__':
main()