From c6f3628b61cb8b0f578e91365521ef1bdd88431f Mon Sep 17 00:00:00 2001 From: dbouget Date: Tue, 10 Sep 2024 14:39:01 +0200 Subject: [PATCH 1/2] Flag to automatically replace spaces in destination filename. Preventing crash when suprocessing using filenames with spaces. [skip ci] --- vsi2tif/src/convert.py | 4 ++-- vsi2tif/src/process.py | 6 +++++- vsi2tif/vsi2tif.py | 2 ++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/vsi2tif/src/convert.py b/vsi2tif/src/convert.py index 060a027..e3c4879 100644 --- a/vsi2tif/src/convert.py +++ b/vsi2tif/src/convert.py @@ -22,7 +22,7 @@ def cellsens2raw( cmd = ( f"{bfconvert} -tilex {tz} -tiley {tz} -nogroup -no-upgrade -overwrite -bigtiff -series {plane} " - f"-compression {compression} {input_path} {output_path}" + f"-compression {compression} \"{input_path}\" \"{output_path}\"" ) try: run_wrapper(cmd=cmd, verbose=verbose, max_mem=max_mem) @@ -37,7 +37,7 @@ def raw2tif(input_path: str, output_path: str, compression: str = "jpeg", qualit os.makedirs("/".join(output_path.split("/")[:-1]), exist_ok=True) cmd = ( - f"vips tiffsave {input_path} {output_path} --bigtiff --tile --pyramid --compression={compression} --Q={quality}" + f"vips tiffsave \"{input_path}\" \"{output_path}\" --bigtiff --tile --pyramid --compression={compression} --Q={quality}" ) try: run_wrapper(cmd=cmd, verbose=verbose) diff --git a/vsi2tif/src/process.py b/vsi2tif/src/process.py index 5ea05d3..0e111b2 100644 --- a/vsi2tif/src/process.py +++ b/vsi2tif/src/process.py @@ -32,6 +32,7 @@ def cellsens2tif_batch( quality: int = 85, max_mem: int = 32, verbose: int = 1, + remove_name_spaces = False, ) -> None: # create directory if it does not exist os.makedirs(output_path, exist_ok=True) @@ -48,7 +49,10 @@ def cellsens2tif_batch( # perform conversion in separate processes for root, file in tqdm(paths): curr_input_path = os.path.join(root, file) - curr_output_path = (output_path + "/" + curr_input_path.split(input_path)[-1]).replace(".vsi", ".tif") + if remove_name_spaces: + curr_output_path = (output_path + "/" + curr_input_path.split(input_path)[-1]).replace(' ', '_').replace(".vsi", ".tif") + else: + curr_output_path = (output_path + "/" + curr_input_path.split(input_path)[-1]).replace(".vsi", ".tif") try: cellsens2tif( diff --git a/vsi2tif/vsi2tif.py b/vsi2tif/vsi2tif.py index 47dfc5d..1b39f02 100644 --- a/vsi2tif/vsi2tif.py +++ b/vsi2tif/vsi2tif.py @@ -17,6 +17,7 @@ def main(): parser.add_argument("-q", "--quality", help="compression quality used with JPEG compression", default=85) parser.add_argument("-m", "--max-mem", help="set maximum memory in the java vm", default=32) parser.add_argument("-v", "--verbose", help="set verbosity level", default=1, type=int) + parser.add_argument("-rs", "--remove-name-spaces", help="replace spaces in filename with underscores in batch mode", default=False) argv = parser.parse_args() if argv.verbose not in list(range(6)): @@ -54,6 +55,7 @@ def main(): argv.quality, argv.max_mem, argv.verbose, + argv.remove_name_spaces ) From e5cdf5e989a31565fb9dd8fbd5668b8776343e70 Mon Sep 17 00:00:00 2001 From: dbouget Date: Tue, 10 Sep 2024 16:55:20 +0200 Subject: [PATCH 2/2] linting and flag fix [skip ci] --- vsi2tif/src/convert.py | 5 +++-- vsi2tif/src/process.py | 6 ++++-- vsi2tif/vsi2tif.py | 6 ++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/vsi2tif/src/convert.py b/vsi2tif/src/convert.py index e3c4879..f1de4d6 100644 --- a/vsi2tif/src/convert.py +++ b/vsi2tif/src/convert.py @@ -22,7 +22,7 @@ def cellsens2raw( cmd = ( f"{bfconvert} -tilex {tz} -tiley {tz} -nogroup -no-upgrade -overwrite -bigtiff -series {plane} " - f"-compression {compression} \"{input_path}\" \"{output_path}\"" + f'-compression {compression} "{input_path}" "{output_path}"' ) try: run_wrapper(cmd=cmd, verbose=verbose, max_mem=max_mem) @@ -37,7 +37,8 @@ def raw2tif(input_path: str, output_path: str, compression: str = "jpeg", qualit os.makedirs("/".join(output_path.split("/")[:-1]), exist_ok=True) cmd = ( - f"vips tiffsave \"{input_path}\" \"{output_path}\" --bigtiff --tile --pyramid --compression={compression} --Q={quality}" + f'vips tiffsave "{input_path}" "{output_path}" --bigtiff --tile --pyramid --compression={compression}' + f" --Q={quality}" ) try: run_wrapper(cmd=cmd, verbose=verbose) diff --git a/vsi2tif/src/process.py b/vsi2tif/src/process.py index 0e111b2..1f77366 100644 --- a/vsi2tif/src/process.py +++ b/vsi2tif/src/process.py @@ -32,7 +32,7 @@ def cellsens2tif_batch( quality: int = 85, max_mem: int = 32, verbose: int = 1, - remove_name_spaces = False, + remove_name_spaces=False, ) -> None: # create directory if it does not exist os.makedirs(output_path, exist_ok=True) @@ -50,7 +50,9 @@ def cellsens2tif_batch( for root, file in tqdm(paths): curr_input_path = os.path.join(root, file) if remove_name_spaces: - curr_output_path = (output_path + "/" + curr_input_path.split(input_path)[-1]).replace(' ', '_').replace(".vsi", ".tif") + curr_output_path = ( + (output_path + "/" + curr_input_path.split(input_path)[-1]).replace(" ", "_").replace(".vsi", ".tif") + ) else: curr_output_path = (output_path + "/" + curr_input_path.split(input_path)[-1]).replace(".vsi", ".tif") diff --git a/vsi2tif/vsi2tif.py b/vsi2tif/vsi2tif.py index 1b39f02..1506240 100644 --- a/vsi2tif/vsi2tif.py +++ b/vsi2tif/vsi2tif.py @@ -17,7 +17,9 @@ def main(): parser.add_argument("-q", "--quality", help="compression quality used with JPEG compression", default=85) parser.add_argument("-m", "--max-mem", help="set maximum memory in the java vm", default=32) parser.add_argument("-v", "--verbose", help="set verbosity level", default=1, type=int) - parser.add_argument("-rs", "--remove-name-spaces", help="replace spaces in filename with underscores in batch mode", default=False) + parser.add_argument( + "--remove-name-spaces", help="replace spaces in filename with underscores in batch mode", action="store_true" + ) argv = parser.parse_args() if argv.verbose not in list(range(6)): @@ -55,7 +57,7 @@ def main(): argv.quality, argv.max_mem, argv.verbose, - argv.remove_name_spaces + argv.remove_name_spaces, )