Skip to content

Commit

Permalink
Adds downsampling to export_wkw_as_tiff (#124)
Browse files Browse the repository at this point in the history
* adds downsampling to export_wkw_as_tiff
* fixes
  • Loading branch information
normanrz authored and bulldozer-boy[bot] committed Aug 20, 2019
1 parent d4d06c1 commit 227c7f2
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions wkcuber/export_wkw_as_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from math import ceil
import numpy as np
from PIL import Image
from scipy.ndimage.interpolation import zoom
from typing import Tuple, Dict, Union, List

from wkcuber.metadata import read_metadata_for_layer
Expand Down Expand Up @@ -53,6 +54,10 @@ def create_parser():
"--mag", "-m", help="The magnification that should be read", default=1
)

parser.add_argument(
"--downsample", help="Downsample each tiff image", default=1, type=int
)

tiling_option_group = parser.add_mutually_exclusive_group()

tiling_option_group.add_argument(
Expand Down Expand Up @@ -80,12 +85,12 @@ def create_parser():

def wkw_name_and_bbox_to_tiff_name(name: str, slice_index: int) -> str:
if name is None or name == "":
return f"{slice_index}.tiff"
return f"{slice_index:06d}.tiff"
else:
return f"{name}_{slice_index}.tiff"
return f"{name}_{slice_index:06d}.tiff"


def wkw_slice_to_image(data_slice: np.ndarray) -> Image:
def wkw_slice_to_image(data_slice: np.ndarray, downsample: int = 1) -> Image:
if data_slice.shape[0] == 1:
# discard greyscale dimension
data_slice = data_slice.squeeze(axis=0)
Expand All @@ -94,6 +99,18 @@ def wkw_slice_to_image(data_slice: np.ndarray) -> Image:
else:
# swap axis and move the channel axis
data_slice = data_slice.transpose((2, 1, 0))

if downsample > 1:
data_slice = zoom(
data_slice,
1 / downsample,
output=data_slice.dtype,
order=1,
# this does not mean nearest interpolation,
# it corresponds to how the borders are treated.
mode="nearest",
prefilter=True,
)
return Image.fromarray(data_slice)


Expand All @@ -107,6 +124,7 @@ def export_tiff_slice(
str,
Union[None, Tuple[int, int]],
int,
int,
],
]
):
Expand All @@ -117,6 +135,7 @@ def export_tiff_slice(
dataset_path,
tiling_size,
batch_size,
downsample,
) = export_args
number_of_slices = min(tiff_bbox["size"][2] - batch_number * batch_size, batch_size)
tiff_bbox["size"] = [tiff_bbox["size"][0], tiff_bbox["size"][1], number_of_slices]
Expand All @@ -138,7 +157,7 @@ def export_tiff_slice(

tiff_file_path = os.path.join(dest_path, tiff_file_name)

image = wkw_slice_to_image(tiff_data[:, :, :, slice_index])
image = wkw_slice_to_image(tiff_data[:, :, :, slice_index], downsample)
image.save(tiff_file_path)
logging.info(f"saved slice {slice_name_number}")

Expand All @@ -162,7 +181,8 @@ def export_tiff_slice(
* tiling_size[1] : (y_tile_index + 1)
* tiling_size[1],
slice_index,
]
],
downsample,
)

tile_image.save(
Expand All @@ -183,6 +203,7 @@ def export_tiff_stack(
name,
tiling_slice_size,
batch_size,
downsample,
args,
):
os.makedirs(destination_path, exist_ok=True)
Expand All @@ -201,6 +222,7 @@ def export_tiff_stack(
dataset_path,
tiling_slice_size,
batch_size,
downsample,
)
]
* num_slices,
Expand All @@ -214,7 +236,8 @@ def export_wkw_as_tiff(args):
logging.basicConfig(level=logging.DEBUG)

if args.bbox is None:
_, _, bbox, _ = read_metadata_for_layer(args.source_path, args.layer_name)
_, _, bbox, origin = read_metadata_for_layer(args.source_path, args.layer_name)
bbox = {"topleft": origin, "size": bbox}
else:
bbox = [int(s.strip()) for s in args.bbox.split(",")]
assert len(bbox) == 6
Expand Down Expand Up @@ -248,6 +271,7 @@ def export_wkw_as_tiff(args):
name=args.name,
tiling_slice_size=args.tile_size,
batch_size=args.batch_size,
downsample=args.downsample,
args=args,
)

Expand Down

0 comments on commit 227c7f2

Please sign in to comment.