mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/file-setup.git
				synced 2025-10-31 18:52:44 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			62 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			62 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
| #!/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
 | |
| import os
 | |
| import subprocess
 | |
| 
 | |
| 
 | |
| def parse_arguments():
 | |
|     parser = argparse.ArgumentParser(description='Merges given input images '
 | |
|                                                  'into one multipage tiff.')
 | |
|     parser.add_argument('-i',
 | |
|                         dest='input_dir',
 | |
|                         required=True,
 | |
|                         type=os.path.abspath)
 | |
|     parser.add_argument('-o',
 | |
|                         dest='output_dir',
 | |
|                         required=True,
 | |
|                         type=os.path.abspath)
 | |
|     parser.add_argument('--zip',
 | |
|                         default='combines-pictures',
 | |
|                         dest='zip',
 | |
|                         type=str,
 | |
|                         help='''package result files in zip bundles and set a
 | |
|                                 filename prefix as a string.''',
 | |
|                         required=False)
 | |
|     return parser.parse_args()
 | |
| 
 | |
| 
 | |
| def merge_images(input_dir, output_dir, 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 @{i}/file_list.txt {o}/combined.tif'.format(i=input_dir, o=output_dir)
 | |
|     subprocess.run(cmd, shell=True)
 | |
|     cmd = 'rm {i}/file_list.txt'.format(i=input_dir)
 | |
|     subprocess.run(cmd, shell=True)
 | |
|     if zip:
 | |
|         cmd = 'cd {o} && zip -m {f}-combined.zip combined.tif && cd -'.format(o=output_dir, f=zip)
 | |
|         subprocess.run(cmd, shell=True)
 | |
| 
 | |
| 
 | |
| def main():
 | |
|     args = parse_arguments()
 | |
|     merge_images(args.input_dir, args.output_dir, args.zip)
 | |
| 
 | |
| 
 | |
| if __name__ == '__main__':
 | |
|     main()
 |