From 73aea90d973b085a3d52de586b37fc80aaaeab53 Mon Sep 17 00:00:00 2001 From: Gigon Bae Date: Fri, 3 Jun 2022 19:16:20 -0700 Subject: [PATCH 1/2] Support no-compression method in converter If you want to use no compression method for your converted file, you can set the compression method to None in the converter. In CLI, you can use `--compression` option and set it to any string except `jpeg`. E.g, cucim convert --tile-size 512 --overlap 0 --num-workers 12 \ --output-filename resize.tiff --compression RAW \ notebooks/input/TUPAC-TR-467.svs The converter will then convert the file without any compression. Signed-off-by: Gigon Bae --- python/cucim/src/cucim/clara/cli.py | 9 +++++++-- python/cucim/src/cucim/clara/converter/tiff.py | 17 ++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) 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 d3cfcddbb..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 @@ -162,7 +173,7 @@ def svs2tif(input_file, output_folder, tile_size, overlap, else: subfiletype = SUBFILETYPE_NONE - tif.save( + tif.write( src_arr, software="Glencoe/Faas pyramid", metadata={"axes": "YXC"}, @@ -174,7 +185,7 @@ def svs2tif(input_file, output_folder, tile_size, overlap, y_resolution // 2 ** level, resolution_unit, ), - compress=("jpeg", 95), # requires imagecodecs + compression=compression, # requires imagecodecs subfiletype=subfiletype, ) logger.info("Done.") From 49e817de0c10d229d7669f0abe604a6df2d0edef Mon Sep 17 00:00:00 2001 From: Gregory Lee Date: Fri, 18 Nov 2022 12:26:06 -0500 Subject: [PATCH 2/2] fix bug in positional argument passing. update the test case to use kwargs --- .../tests/unit/clara/converter/test_converter.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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: