diff --git a/vsi2tif/src/process.py b/vsi2tif/src/process.py index 1f77366..62838f5 100644 --- a/vsi2tif/src/process.py +++ b/vsi2tif/src/process.py @@ -1,4 +1,7 @@ +import logging import os +import shutil +import traceback from tqdm import tqdm @@ -18,7 +21,35 @@ def cellsens2tif_single( max_mem: int = 32, verbose: int = 1, ) -> None: - cellsens2tif(input_path, output_path, bfconvert, compression, tz, plane, quality, max_mem, verbose) + + if int(plane) == -1: + image_folder = os.path.join(os.path.dirname(output_path), os.path.basename(output_path).replace(".tif", "")) + for s in range(50): + try: + curr_output_path = os.path.join(image_folder, "plane_" + str(s) + "_" + os.path.basename(output_path)) + cellsens2tif(input_path, curr_output_path, bfconvert, compression, tz, s, quality, max_mem, verbose) + except Exception: + logging.info("End of planes with value {}".format(s)) + break + + try: + largest_size = 0 + largest_file = None + for _, _, files in os.walk(image_folder): + for f in files: + fp = os.path.join(image_folder, f) + fs = os.path.getsize(fp) + if fs > largest_size: + largest_size = fs + largest_file = fp + shutil.copyfile(largest_file, output_path) + if os.path.exists(image_folder): + shutil.rmtree(image_folder) + except Exception: + logging.error("Issue cleaning up after all planes conversion.") + logging.error(traceback.format_exc()) + else: + cellsens2tif(input_path, output_path, bfconvert, compression, tz, plane, quality, max_mem, verbose) @benchmark @@ -57,7 +88,7 @@ def cellsens2tif_batch( curr_output_path = (output_path + "/" + curr_input_path.split(input_path)[-1]).replace(".vsi", ".tif") try: - cellsens2tif( + cellsens2tif_single( curr_input_path, curr_output_path, bfconvert, compression, tz, plane, quality, max_mem, verbose ) except Exception: diff --git a/vsi2tif/vsi2tif.py b/vsi2tif/vsi2tif.py index 1506240..4106237 100644 --- a/vsi2tif/vsi2tif.py +++ b/vsi2tif/vsi2tif.py @@ -13,7 +13,13 @@ def main(): parser.add_argument("-b", "--bfconvert", help="path to bfconvert tool", required=True) parser.add_argument("-c", "--compression", help="compression technique for final image", default="jpeg") parser.add_argument("-s", "--tilesize", help="tile size to use during both conversion steps", default=1024) - parser.add_argument("-p", "--plane", help="which image plane to convert image from", default=0) + parser.add_argument( + "-p", + "--plane", + help="which image plane to convert image from. If set to -1, all planes are converted and the largest is kept", + default=0, + type=int, + ) 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)