Last active
September 7, 2020 08:25
-
-
Save gatoniel/a2b60392086eb8e835eb9a995336cb29 to your computer and use it in GitHub Desktop.
Create Tiff-File with series.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from tifffile import TiffWriter, imread | |
def create_tif_series(file, list_of_imgs, axes): | |
""" | |
Creates a tif file that includes len(list_of_imgs) series. | |
Arguments: | |
file: str or File like object, where tif file is written to. | |
list_of_imgs: list of numpy arrays. The imgs, that are written | |
axes: list of str. The axes of the imgs, that are written | |
Returns: | |
None | |
""" | |
with TiffWriter(file) as tif: | |
for img, ax in zip(list_of_imgs, axes): | |
if not img.ndim == len(ax): | |
img = np.expand_dims(img, axis=ax.index("C")) | |
tif.save(img, metadata={"axes": ax}, contiguous=False) | |
def create_tif_series_from_path(file, path, axes="CYX"): | |
""" | |
Writes all tif files of a path into a single tif file. Each tif file | |
into a different series. | |
Arguments: | |
file: str of File like object, where tif file is written to | |
path: path where tif files are saved | |
axes: the axes of the files in that path | |
Returns: | |
None | |
""" | |
imgs = sorted(list(os.listdir(path))) | |
imgs = [imread(os.path.join(path, img)) for img in imgs] | |
print("imgs[0].shape: ", imgs[0].shape) | |
print("using axes :", axes) | |
create_tif_series(file, imgs, [axes,]*len(imgs)) | |
def create_tifs_from_paths(path, axes="CYX", file_ending=".ome.tiff"): | |
""" | |
Creates tif files from paths. | |
Arguments: | |
path: The path where more path with | |
corresponding tif files reside. | |
Returns: | |
None | |
""" | |
paths = list(os.listdir(path)) | |
paths = [ | |
p for p in paths if os.path.isdir( | |
os.path.join(path, p) | |
) | |
] | |
files = [p + file_ending for p in paths] | |
for f, p in zip(files, paths): | |
create_tif_series_from_path( | |
os.path.join(path, f), | |
os.path.join(path, p), | |
axes | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment