diff --git a/python/cucim/src/cucim/clara/cli.py b/python/cucim/src/cucim/clara/cli.py index c4faceebc..c6011798b 100644 --- a/python/cucim/src/cucim/clara/cli.py +++ b/python/cucim/src/cucim/clara/cli.py @@ -49,12 +49,17 @@ def main(): @click.option('--tile-size', type=int, default=256) @click.option('--overlap', type=int, default=0) @click.option('--num-workers', type=int, default=os.cpu_count()) +@click.option('--compression', type=str, default='jpeg') @click.option('--output-filename', type=str, default='image.tif') def convert(src_file, dest_folder, tile_size, overlap, num_workers, - output_filename): + compression, output_filename): """Convert file format""" from .converter import tiff logging.basicConfig(level=logging.INFO) + compression = compression.lower() + if compression in ['raw', 'none']: + compression = None + tiff.svs2tif(src_file, Path(dest_folder), tile_size, overlap, num_workers, - output_filename) + compression, output_filename) diff --git a/python/cucim/src/cucim/clara/converter/tiff.py b/python/cucim/src/cucim/clara/converter/tiff.py index d053d341e..feb6012a5 100644 --- a/python/cucim/src/cucim/clara/converter/tiff.py +++ b/python/cucim/src/cucim/clara/converter/tiff.py @@ -56,7 +56,8 @@ def filter_tile( def svs2tif(input_file, output_folder, tile_size, overlap, - num_workers=os.cpu_count(), output_filename="image.tif"): + num_workers=os.cpu_count(), compression="jpeg", + output_filename="image.tif"): output_folder = str(output_folder) logger.info("Parameters") @@ -65,8 +66,18 @@ def svs2tif(input_file, output_folder, tile_size, overlap, logger.info(" tile size: %d", tile_size) logger.info(" overlap: %d", overlap) logger.info(" num_workers: %d", num_workers) + logger.info(" compression: %s", compression) logger.info(" output filename: %s", output_filename) + if compression is not None: + # handles only jpeg or None (no compression) + if compression.lower() == "jpeg": + compression = ("jpeg", 95) + else: + raise ValueError( + f"Unsupported compression: {compression}." + + " Should be 'jpeg' or None.") + with OpenSlide(input_file) as slide: properties = slide.properties slide_dimensions = slide.dimensions @@ -174,7 +185,7 @@ def svs2tif(input_file, output_folder, tile_size, overlap, y_resolution // 2 ** level, resolution_unit, ), - compression=("jpeg", 95), # requires imagecodecs + compression=compression, # requires imagecodecs subfiletype=subfiletype, ) logger.info("Done.") diff --git a/python/cucim/tests/unit/clara/converter/test_converter.py b/python/cucim/tests/unit/clara/converter/test_converter.py index bc0592474..fa5bc30c3 100644 --- a/python/cucim/tests/unit/clara/converter/test_converter.py +++ b/python/cucim/tests/unit/clara/converter/test_converter.py @@ -29,8 +29,15 @@ def test_image_converter_stripe_4096x4096_256_jpeg( num_workers = os.cpu_count() file_name = "test_image_converter_stripe_4096x4096_256_jpeg-output.tif" output_path = tmp_path / file_name - tiff.svs2tif(testimg_tiff_stripe_4096x4096_256_jpeg, Path(tmp_path), - tile_size, overlap, num_workers, file_name) + tiff.svs2tif( + testimg_tiff_stripe_4096x4096_256_jpeg, + output_folder=Path(tmp_path), + tile_size=tile_size, + overlap=overlap, + num_workers=num_workers, + output_filename=file_name, + ) + assert os.path.exists(output_path) with tifffile.TiffFile(output_path) as tif: