file-setup/file_setup
Patrick Jentsch 053366aa09 Bump versions
2020-04-06 09:22:04 +02:00

62 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3.7
# coding=utf-8
"""
merge_images
Usage: For usage instructions run with option --help
Author: Stephan Porada <sporada@uni-bielefeld.de>
"""
from argparse import ArgumentParser
import os
import subprocess
def parse_arguments():
parser = ArgumentParser(description='Merges images into one .pdf file.')
parser.add_argument('i', metavar='input directory')
parser.add_argument('o', metavar='output directory')
parser.add_argument('f', metavar='output file base')
parser.add_argument('--log-dir')
parser.add_argument('--zip')
return parser.parse_args()
def merge_images(input_dir, output_dir, output_file_base, zip):
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)
cmd = 'convert @"{}"/file_list.txt "{}"/"{}".pdf'.format(input_dir, output_dir, output_file_base) # noqa
subprocess.run(cmd, shell=True)
cmd = 'rm "{}"/file_list.txt'.format(input_dir)
subprocess.run(cmd, shell=True)
if zip is not None:
# Remove .zip file extension if provided
if zip.endswith('.zip'):
zip = zip[:-4]
zip = zip if zip else 'output'
cmd = 'cd "{}"'.format(output_dir)
cmd += ' && '
cmd += 'zip'
cmd += ' -m'
cmd += ' "{}".zip "{}".pdf'.format(zip, output_file_base)
cmd += ' && '
cmd += 'cd -'
subprocess.run(cmd, shell=True)
def main():
args = parse_arguments()
print(args)
merge_images(args.i, args.o, args.f, args.zip)
if __name__ == '__main__':
main()