-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add hologram file conversion example
- Loading branch information
1 parent
1443dfa
commit 4e51a2d
Showing
4 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,3 +105,4 @@ _version_save.py | |
*.bib.bak | ||
examples/*.tif | ||
examples/*.hdf5 | ||
.pytest_cache |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"""Conversion of external holograms to .tif files | ||
Qpformat can load hologram data from .tif image files. If your | ||
experimental hologram data are stored in a different file format, | ||
you can either request its implementation in qpformat by | ||
`creating an issue<https://github.com/RI-imaging/qpformat/issues/new>`_ | ||
or you can modify this example script to your needs. | ||
This example must be executed with a directory as an | ||
command line argument, i.e. | ||
``python convert_txt2tif /path/to/folder/`` | ||
""" | ||
import pathlib | ||
import sys | ||
|
||
import numpy as np | ||
from skimage.external import tifffile | ||
|
||
# File names ending with these strings are ignored | ||
ignore_endswith = ['.bmp', '.npy', '.opj', '.png', '.pptx', '.py', '.svg', | ||
'.tif', '.txt', '_RIdist', '_parameter', '_parameter_old', | ||
'_phase', 'n_array', 'n_array_real', '~'] | ||
|
||
|
||
def get_paths(folder, ignore_endswith=ignore_endswith): | ||
"""Return hologram file paths | ||
Parameters | ||
---------- | ||
folder: str or pathlib.Path | ||
Path to search folder | ||
ignore_endswith: list | ||
List of filename ending strings indicating which | ||
files should be ignored. | ||
""" | ||
folder = pathlib.Path(folder).resolve() | ||
files = folder.rglob("*") | ||
for ie in ignore_endswith: | ||
files = [ff for ff in files if not ff.name.endswith(ie)] | ||
return sorted(files) | ||
|
||
|
||
if __name__ == "__main__": | ||
path = pathlib.Path(sys.argv[-1]) | ||
if not path.is_dir(): | ||
raise ValueError("Command line argument must be directory!") | ||
# output directory | ||
pout = path.parent / (path.name + "_tif") | ||
pout.mkdir(exist_ok=True) | ||
# get input hologram files | ||
files = get_paths(path) | ||
# conversion | ||
for ff in files: | ||
# convert image data to uint8 (most image sensors) | ||
hol = np.loadtxt(str(ff), dtype=np.uint8) | ||
tifout = str(pout / (ff.name[:-10] + ".tif")) | ||
# compress image data | ||
tifffile.imsave(tifout, hol, compress=9) |