Skip to content

Commit 71652be

Browse files
authored
Added option to save extracted block as TIF (#28)
Added option to save extracted block as TIF
1 parent aee6121 commit 71652be

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

scripts/extract_block.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
from typing import Optional, List
77

8+
import imageio.v3 as imageio
89
import numpy as np
910
import zarr
1011

@@ -16,8 +17,10 @@ def main(
1617
coords: List[int],
1718
output_dir: str = None,
1819
input_key: str = "setup0/timepoint0/s0",
20+
output_key: Optional[str] = None,
1921
resolution: float = 0.38,
2022
roi_halo: List[int] = [128, 128, 64],
23+
tif: bool = False,
2124
s3: Optional[bool] = False,
2225
s3_credentials: Optional[str] = None,
2326
s3_bucket_name: Optional[str] = None,
@@ -30,14 +33,17 @@ def main(
3033
input_path: Input folder in n5 / ome-zarr format.
3134
coords: Center coordinates of extracted 3D volume.
3235
output_dir: Output directory for saving output as <basename>_crop.n5. Default: input directory.
36+
input_key: Input key for data in input file.
37+
output_key: Output key for data in n5 output or used as suffix for tif output.
3338
roi_halo: ROI halo of extracted 3D volume.
39+
tif: Flag for tif output
3440
s3: Flag for considering input_path for S3 bucket.
3541
s3_bucket_name: S3 bucket name.
3642
s3_service_endpoint: S3 service endpoint.
3743
s3_credentials: File path to credentials for S3 bucket.
3844
"""
39-
40-
coord_string = "-".join([str(c) for c in coords])
45+
coords = [int(round(c)) for c in coords]
46+
coord_string = "-".join([str(c).zfill(4) for c in coords])
4147

4248
# Dimensions are inversed to view in MoBIE (x y z) -> (z y x)
4349
coords.reverse()
@@ -46,7 +52,14 @@ def main(
4652
input_content = list(filter(None, input_path.split("/")))
4753

4854
if s3:
49-
basename = input_content[0] + "_" + input_content[-1].split(".")[0]
55+
image_name = input_content[-1].split(".")[0]
56+
if len(image_name.split("_")) > 1:
57+
resized_suffix = "_resized"
58+
image_prefix = image_name.split("_")[0]
59+
else:
60+
resized_suffix = ""
61+
image_prefix = image_name
62+
basename = input_content[0] + resized_suffix
5063
else:
5164
basename = "".join(input_content[-1].split(".")[:-1])
5265

@@ -56,7 +69,16 @@ def main(
5669
if output_dir == "":
5770
output_dir = input_dir
5871

59-
output_file = os.path.join(output_dir, basename + "_crop_" + coord_string + ".n5")
72+
if tif:
73+
if output_key is None:
74+
output_name = basename + "_crop_" + coord_string + "_" + image_prefix + ".tif"
75+
else:
76+
output_name = basename + "_" + image_prefix + "_crop_" + coord_string + "_" + output_key + ".tif"
77+
78+
output_file = os.path.join(output_dir, output_name)
79+
else:
80+
output_key = "raw" if output_key is None else output_key
81+
output_file = os.path.join(output_dir, basename + "_crop_" + coord_string + ".n5")
6082

6183
coords = np.array(coords)
6284
coords = coords / resolution
@@ -75,26 +97,31 @@ def main(
7597
with zarr.open(input_path, mode="r") as f:
7698
raw = f[input_key][roi]
7799

78-
with zarr.open(output_file, mode="w") as f_out:
79-
f_out.create_dataset("raw", data=raw, compression="gzip")
100+
if tif:
101+
imageio.imwrite(output_file, raw, compression="zlib")
102+
else:
103+
with zarr.open(output_file, mode="w") as f_out:
104+
f_out.create_dataset(output_key, data=raw, compression="gzip")
80105

81106

82107
if __name__ == "__main__":
83108

84109
parser = argparse.ArgumentParser(
85110
description="Script to extract region of interest (ROI) block around center coordinate.")
86111

87-
parser.add_argument('-i', '--input', type=str, help="Input file in n5 / ome-zarr format.")
112+
parser.add_argument('-i', '--input', type=str, required=True, help="Input file in n5 / ome-zarr format.")
88113
parser.add_argument('-o', "--output", type=str, default="", help="Output directory.")
89114
parser.add_argument('-c', "--coord", type=str, required=True,
90115
help="3D coordinate as center of extracted block, json-encoded.")
91116

92117
parser.add_argument('-k', "--input_key", type=str, default="setup0/timepoint0/s0",
93118
help="Input key for data in input file.")
119+
parser.add_argument("--output_key", type=str, default=None,
120+
help="Output key for data in output file.")
94121
parser.add_argument('-r', "--resolution", type=float, default=0.38, help="Resolution of input in micrometer.")
95-
96122
parser.add_argument("--roi_halo", type=str, default="[128,128,64]",
97123
help="ROI halo around center coordinate, json-encoded.")
124+
parser.add_argument("--tif", action="store_true", help="Store output as tif file.")
98125

99126
parser.add_argument("--s3", action="store_true", help="Use S3 bucket.")
100127
parser.add_argument("--s3_credentials", type=str, default=None,
@@ -111,6 +138,6 @@ def main(
111138
roi_halo = json.loads(args.roi_halo)
112139

113140
main(
114-
args.input, coords, args.output, args.input_key, args.resolution, roi_halo,
115-
args.s3, args.s3_credentials, args.s3_bucket_name, args.s3_service_endpoint,
141+
args.input, coords, args.output, args.input_key, args.output_key, args.resolution, roi_halo,
142+
args.tif, args.s3, args.s3_credentials, args.s3_bucket_name, args.s3_service_endpoint,
116143
)

0 commit comments

Comments
 (0)