Python tifffile.TiffFile() Examples
The following are 12
code examples of tifffile.TiffFile().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tifffile
, or try the search function
.
Example #1
Source File: TIF.py From ClearMap with GNU General Public License v3.0 | 6 votes |
def dataSize(filename, **args): """Returns size of data in tif file Arguments: filename (str): file name as regular expression x,y,z (tuple): data range specifications Returns: tuple: data size """ t = tiff.TiffFile(filename); d3 = len(t.pages); d2 = t.pages[0].shape; #d2 = (d2[0], d2[1]); if len(d2) == 3: d2 = (d2[2], d2[1], d2[0]); else: d2 = (d2[1], d2[0]); if d3 > 1: dims = d2 + (d3,); else: dims = d2; return io.dataSizeFromDataRange(dims, **args);
Example #2
Source File: TIF.py From ClearMap with GNU General Public License v3.0 | 6 votes |
def dataZSize(filename, z = all, **args): """Returns z size of data in tif file Arguments: filename (str): file name as regular expression z (tuple): z data range specification Returns: int: z data size """ t = tiff.TiffFile(filename); d2 = t.pages[0].shape; if len(d2) == 3: return io.toDataSize(d2[0], r = z); d3 = len(t.pages); if d3 > 1: return io.toDataSize(d3, r = z); else: return None;
Example #3
Source File: tiff_reader.py From aicsimageio with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _read_immediate(self) -> np.ndarray: # Load Tiff with TiffFile(self._file) as tiff: # Check each scene has the same shape # If scene shape checking fails, use the specified scene and update # operating shape scenes = tiff.series if not self._scene_shape_is_consistent(tiff, S=self.specific_s_index): return scenes[self.specific_s_index].asarray() # Read each scene and stack if single scene if len(scenes) > 1: return np.stack([s.asarray() for s in scenes]) # Else, return single scene return tiff.asarray()
Example #4
Source File: tiff.py From suite2p with GNU General Public License v3.0 | 6 votes |
def open_tiff(file, sktiff): """ opens tiff with either ScanImageTiffReader or tifffile returns tiff and its length """ if sktiff: tif = TiffFile(file) Ltif = len(tif.pages) else: tif = ScanImageTiffReader(file) tsize = tif.shape() if len(tsize) < 3: # single page tiffs Ltif = 1 else: Ltif = tif.shape()[0] return tif, Ltif
Example #5
Source File: ome_tiff_reader.py From aicsimageio with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _lazy_init_metadata(self) -> omexml.OMEXML: with TiffFile(self._file) as tiff: if self._metadata is None and tiff.is_ome: description = tiff.pages[0].description.strip() if not ( description.startswith("<?xml version=") and description.endswith("</OME>") ): raise ValueError( f"Description does not conform to OME specification: " f"{description[:100]}" ) self._metadata = omexml.OMEXML(description) return self._metadata
Example #6
Source File: tiff_reader.py From aicsimageio with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _imread(img: Path, scene: int, page: int) -> np.ndarray: # Load Tiff with TiffFile(img) as tiff: # Get proper scene scene = tiff.series[scene] # Get proper page page = scene.pages[page] # Return numpy return page.asarray()
Example #7
Source File: tiff_reader.py From aicsimageio with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _scene_shape_is_consistent(tiff: TiffFile, S: int) -> bool: scenes = tiff.series operating_shape = scenes[0].shape for scene in scenes: if scene.shape != operating_shape: log.info( f"File contains variable dimensions per scene, " f"selected scene: {S} for data " f"retrieval." ) return False return True
Example #8
Source File: tiff_reader.py From aicsimageio with BSD 3-Clause "New" or "Revised" License | 5 votes |
def dtype(self): if self._dtype is None: with TiffFile(self._file) as tiff: self._dtype = tiff.pages[0].dtype return self._dtype
Example #9
Source File: utilities.py From minian with GNU General Public License v3.0 | 5 votes |
def load_tif_lazy(fname): with TiffFile(fname) as tif: data = tif.asarray() f = int(data.shape[0]) fmread = da.delayed(load_tif_perframe) flist = [fmread(fname, i) for i in range(f)] sample = flist[0].compute() arr = [da.array.from_delayed( fm, dtype=sample.dtype, shape=sample.shape) for fm in flist] return da.array.stack(arr, axis=0)
Example #10
Source File: TIF.py From ClearMap with GNU General Public License v3.0 | 4 votes |
def readData(filename, x = all, y = all, z = all, **args): """Read data from a single tif image or stack Arguments: filename (str): file name as regular expression x,y,z (tuple): data range specifications Returns: array: image data """ dsize = dataSize(filename); #print "dsize %s" % str(dsize); if len(dsize) == 2: data = tiff.imread(filename, key = 0); #print "data.shape %s" % str(data.shape); return io.dataToRange(data.transpose([1,0]), x = x, y = y); #return io.dataToRange(data, x = x, y = y); else: if z is all: data = tiff.imread(filename); if data.ndim == 2: # data = data data = data.transpose([1,0]); elif data.ndim == 3: #data = data.transpose([1,2,0]); data = data.transpose([2,1,0]); elif data.ndim == 4: # multi channel image #data = data.transpose([1,2,0,3]); data = data.transpose([2,1,0,3]); else: raise RuntimeError('readData: dimension %d not supproted!' % data.ndim) return io.dataToRange(data, x = x, y = y, z = all); else: #optimize for z ranges ds = io.dataSizeFromDataRange(dsize, x = x, y = y, z = z); t = tiff.TiffFile(filename); p = t.pages[0]; data = numpy.zeros(ds, dtype = p.dtype); rz = io.toDataRange(dsize[2], r = z); #print "test" #print rz; #print dsize for i in range(rz[0], rz[1]): xydata = t.pages[i].asarray(); #data[:,:,i-rz[0]] = io.dataToRange(xydata, x = x, y = y); data[:,:,i-rz[0]] = io.dataToRange(xydata.transpose([1,0]), x = x, y = y); return data
Example #11
Source File: tiff_reader.py From aicsimageio with BSD 3-Clause "New" or "Revised" License | 4 votes |
def _read_delayed(self) -> da.core.Array: # Load Tiff with TiffFile(self._file) as tiff: # Check each scene has the same shape # If scene shape checking fails, use the specified scene and update # operating shape scenes = tiff.series operating_shape = scenes[0].shape if not self._scene_shape_is_consistent(tiff, S=self.specific_s_index): operating_shape = scenes[self.specific_s_index].shape scenes = [scenes[self.specific_s_index]] # Get sample yx plane sample = scenes[0].pages[0].asarray() # Combine length of scenes and operating shape # Replace YX dims with empty dimensions operating_shape = (len(scenes), *operating_shape) operating_shape = operating_shape[:-2] + (1, 1) # Make ndarray for lazy arrays to fill lazy_arrays = np.ndarray(operating_shape, dtype=object) for all_page_index, (np_index, _) in enumerate(np.ndenumerate(lazy_arrays)): # Scene index is the first index in np_index scene_index = np_index[0] # This page index is current enumeration divided by scene index + 1 # For example if the image has 10 Z slices and 5 scenes, there # would be 50 total pages this_page_index = all_page_index // (scene_index + 1) # Fill the numpy array with the delayed arrays lazy_arrays[np_index] = da.from_delayed( delayed(TiffReader._imread)( self._file, scene_index, this_page_index ), shape=sample.shape, dtype=sample.dtype, ) # Convert the numpy array of lazy readers into a dask array data = da.block(lazy_arrays.tolist()) # Only return the scene dimension if multiple scenes are present if len(scenes) == 1: data = data[0, :] return data
Example #12
Source File: tiff_reader.py From aicsimageio with BSD 3-Clause "New" or "Revised" License | 4 votes |
def dims(self) -> str: if self._dims is None: # Get a single scenes dimensions in order with TiffFile(self._file) as tiff: single_scene_dims = tiff.series[0].pages.axes # We can sometimes trust the dimension info in the image if all([d in Dimensions.DefaultOrder for d in single_scene_dims]): # Add scene dimension only if there are multiple scenes if len(tiff.series) == 1: self._dims = single_scene_dims else: self._dims = f"{Dimensions.Scene}{single_scene_dims}" # Sometimes the dimension info is wrong in certain dimensions, so guess # that dimension else: guess = self.guess_dim_order(tiff.series[0].pages.shape) best_guess = [] for dim_from_meta in single_scene_dims: if dim_from_meta in Dimensions.DefaultOrder: best_guess.append(dim_from_meta) else: appended_dim = False for guessed_dim in guess: if guessed_dim not in best_guess: best_guess.append(guessed_dim) appended_dim = True log.info( f"Unsure how to handle dimension: " f"{dim_from_meta}. " f"Replaced with guess: {guessed_dim}" ) break # All of our guess dims were already in the dim list, # append the dim read from meta if not appended_dim: best_guess.append(dim_from_meta) best_guess = "".join(best_guess) # Add scene dimension only if there are multiple scenes if len(tiff.series) == 1: self._dims = best_guess else: self._dims = f"{Dimensions.Scene}{best_guess}" return self._dims