55import os
66from typing import Optional , List
77
8+ import imageio .v3 as imageio
89import numpy as np
910import zarr
1011
@@ -16,8 +17,10 @@ def main(
1617 coords : List [int ],
1718 output_dir : str = None ,
1819 input_key : str = "setup0/timepoint0/s0" ,
19- resolution : float = 0.38 ,
20+ output_key : Optional [str ] = None ,
21+ resolution : Optional [float ] = 0.38 ,
2022 roi_halo : List [int ] = [128 , 128 , 64 ],
23+ tif : Optional [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 ()
@@ -56,7 +62,14 @@ def main(
5662 if output_dir == "" :
5763 output_dir = input_dir
5864
59- output_file = os .path .join (output_dir , basename + "_crop_" + coord_string + ".n5" )
65+ if tif :
66+ if output_key is None :
67+ output_file = os .path .join (output_dir , basename + "_crop_" + coord_string + ".tif" )
68+ else :
69+ output_file = os .path .join (output_dir , basename + "_crop_" + coord_string + "_" + output_key + ".tif" )
70+ else :
71+ output_key = "raw" if output_key is None else output_key
72+ output_file = os .path .join (output_dir , basename + "_crop_" + coord_string + ".n5" )
6073
6174 coords = np .array (coords )
6275 coords = coords / resolution
@@ -75,26 +88,31 @@ def main(
7588 with zarr .open (input_path , mode = "r" ) as f :
7689 raw = f [input_key ][roi ]
7790
78- with zarr .open (output_file , mode = "w" ) as f_out :
79- f_out .create_dataset ("raw" , data = raw , compression = "gzip" )
91+ if tif :
92+ imageio .imwrite (output_file , raw )
93+ else :
94+ with zarr .open (output_file , mode = "w" ) as f_out :
95+ f_out .create_dataset (output_key , data = raw , compression = "gzip" )
8096
8197
8298if __name__ == "__main__" :
8399
84100 parser = argparse .ArgumentParser (
85101 description = "Script to extract region of interest (ROI) block around center coordinate." )
86102
87- parser .add_argument ('-i' , '--input' , type = str , help = "Input file in n5 / ome-zarr format." )
103+ parser .add_argument ('-i' , '--input' , type = str , required = True , help = "Input file in n5 / ome-zarr format." )
88104 parser .add_argument ('-o' , "--output" , type = str , default = "" , help = "Output directory." )
89105 parser .add_argument ('-c' , "--coord" , type = str , required = True ,
90106 help = "3D coordinate as center of extracted block, json-encoded." )
91107
92108 parser .add_argument ('-k' , "--input_key" , type = str , default = "setup0/timepoint0/s0" ,
93109 help = "Input key for data in input file." )
110+ parser .add_argument ("--output_key" , type = str , default = None ,
111+ help = "Output key for data in output file." )
94112 parser .add_argument ('-r' , "--resolution" , type = float , default = 0.38 , help = "Resolution of input in micrometer." )
95-
96113 parser .add_argument ("--roi_halo" , type = str , default = "[128,128,64]" ,
97114 help = "ROI halo around center coordinate, json-encoded." )
115+ parser .add_argument ("--tif" , action = "store_true" , help = "Store output as tif file." )
98116
99117 parser .add_argument ("--s3" , action = "store_true" , help = "Use S3 bucket." )
100118 parser .add_argument ("--s3_credentials" , type = str , default = None ,
@@ -111,6 +129,6 @@ def main(
111129 roi_halo = json .loads (args .roi_halo )
112130
113131 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 ,
132+ args .input , coords , args .output , args .input_key , args .output_key , args . resolution , roi_halo ,
133+ args .tif , args . s3 , args .s3_credentials , args .s3_bucket_name , args .s3_service_endpoint ,
116134 )
0 commit comments