-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtifstack.py
More file actions
50 lines (40 loc) · 1.54 KB
/
tifstack.py
File metadata and controls
50 lines (40 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from pathlib import Path
from typing import Union
from tifffile import natural_sorted, imread
from numpy import zeros
# from pystripe.core import imread_tif_raw_png
# From image-processing-pipeline
# build a tif class with similar interface
class TifStack:
"""
We need a tif stack with an interface that will load a slice one at a time
We assume each tif has the same size
"""
def __init__(self, input_directory: Union[Path, str], z_offset: int = 0):
if isinstance(input_directory, str):
input_directory = Path(input_directory)
self.input_directory = input_directory
self.z_offset = z_offset
self.files = [file.__str__() for file in input_directory.iterdir() if
file.is_file() and file.suffix.lower() in (".tif", ".tiff")]
self.files = list(map(Path, natural_sorted(self.files)))
self.suffix = self.files[0].suffix
img = imread(self.files[0])
self.dtype = img.dtype
self.nyx = img.shape
self.nz = len(self.files)
self.shape = (self.nz, self.nyx[0], self.nyx[1]) # stack height, img_y, img_x
def __getitem__(self, i):
i += self.z_offset
if i < 0 or i >= self.nz:
return None
return imread(self.files[i])
def close(self):
pass
def as_3d_numpy(self):
stack = zeros(self.shape, dtype=self.dtype)
for idx in range(self.nz):
stack[idx] = self.__getitem__(idx)
return stack
def imread_tif_stck(tif_stack, idx):
return tif_stack[idx]