1717
1818from loci .plugins .in import ImporterOptions # pdoc: skip
1919
20+ from loci .formats import ImageReader , Memoizer
21+
2022from ..pathtools import gen_name_from_orig
2123from ..log import LOG as log
2224
2325
2426def import_image (
25- filename , color_mode = "color" , split_c = False , split_z = False , split_t = False
27+ filename ,
28+ color_mode = "color" ,
29+ split_c = False ,
30+ split_z = False ,
31+ split_t = False ,
32+ series_number = None ,
33+ c_start = None ,
34+ c_end = None ,
35+ c_interval = None ,
36+ z_start = None ,
37+ z_end = None ,
38+ z_interval = None ,
39+ t_start = None ,
40+ t_end = None ,
41+ t_interval = None ,
2642):
2743 """Open an image file using the Bio-Formats importer.
2844
@@ -39,6 +55,35 @@ def import_image(
3955 Whether to split the z-slices into separate ImagePlus objects.
4056 split_t : bool, optional
4157 Whether to split the time points into separate ImagePlus objects.
58+ series_number : int, optional
59+ open a specific Bio-Formats series
60+ c_start : int, optional
61+ only import a subset of channel starting with this one. Requires to set
62+ c_end and c_interval.
63+ c_end : int, optional
64+ only import channel(s) ending with this one. Requires to set c_start and
65+ c_interval.
66+ c_interval : int, optional
67+ only import a subset of channel with this interval. Requires to set
68+ c_start and c_end.
69+ z_start : int, optional
70+ only import a subset of planes starting with this one. Requires to set
71+ z_end and z_interval.
72+ z_end : int, optional
73+ only import a subset of planes ending with this one. Requires to set
74+ z_start and z_interval.
75+ z_interval : int, optional
76+ only import a subset of planes with this interval. Requires to set
77+ z_start and z_end.
78+ t_start : int, optional
79+ only import a subset of time points starting with this one. Requires to
80+ set t_end and t_interval.
81+ t_end : int, optional
82+ only import a subset of time points ending with this one. Requires to
83+ set t_start and t_interval.
84+ t_interval : int, optional
85+ only import a subset of time points with thsi interval. Requires to set
86+ t_start and t_end.
4287
4388 Returns
4489 -------
@@ -57,6 +102,33 @@ def import_image(
57102 options .setSplitFocalPlanes (split_z )
58103 options .setSplitTimepoints (split_t )
59104 options .setId (filename )
105+ if series_number is not None :
106+ options .setSeriesOn (series_number , True )
107+
108+ if c_start is not None :
109+ if series_number is None :
110+ series_number = 0
111+ options .setSpecifyRanges (True )
112+ options .setCBegin (series_number , c_start )
113+ options .setCEnd (series_number , c_end )
114+ options .setCStep (series_number , c_interval )
115+
116+ if z_start is not None :
117+ if series_number is None :
118+ series_number = 0
119+ options .setSpecifyRanges (True )
120+ options .setZBegin (series_number , z_start )
121+ options .setZEnd (series_number , z_end )
122+ options .setZStep (series_number , z_interval )
123+
124+ if t_start is not None :
125+ if series_number is None :
126+ series_number = 0
127+ options .setSpecifyRanges (True )
128+ options .setTBegin (series_number , t_start )
129+ options .setTEnd (series_number , t_end )
130+ options .setTStep (series_number , t_interval )
131+
60132 log .info ("Reading [%s]" , filename )
61133 orig_imps = BF .openImagePlus (options )
62134 log .debug ("Opened [%s] %s" , filename , type (orig_imps ))
@@ -131,3 +203,44 @@ def export_using_orig_name(imp, path, orig_name, tag, suffix, overwrite=False):
131203 out_file = gen_name_from_orig (path , orig_name , tag , suffix )
132204 export (imp , out_file , overwrite )
133205 return out_file
206+
207+
208+ def get_series_count_from_ome_metadata (path_to_file ):
209+ """Get the Bio-Formates series count from a file on disk.
210+
211+ Useful to access a specific image in a container format like .czi, .nd2, .lif...
212+
213+ Parameters
214+ ----------
215+ path_to_file : str
216+ The full path to the image file.
217+
218+ Returns
219+ -------
220+ int
221+ The number of Bio-Formats series detected in the image file metadata.
222+ """
223+ reader = ImageReader ()
224+ ome_meta = MetadataTools .createOMEXMLMetadata ()
225+ reader .setMetadataStore (ome_meta )
226+ reader .setId (path_to_file )
227+ series_count = reader .getSeriesCount ()
228+ reader .close ()
229+
230+ return series_count
231+
232+
233+ def write_bf_memoryfile (path_to_file ):
234+ """Write a BF memo-file so subsequent access to the same file is faster.
235+
236+ The Bio-Formats memo-file is written next to the image file (i.e. in the
237+ same folder as the given file).
238+
239+ Parameters
240+ ----------
241+ path_to_file : string
242+ The full path to the image file.
243+ """
244+ reader = Memoizer (ImageReader ())
245+ reader .setId (path_to_file )
246+ reader .close ()
0 commit comments