Skip to content

Commit

Permalink
docs: add hologram file conversion example
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Mar 13, 2018
1 parent 1443dfa commit 4e51a2d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ _version_save.py
*.bib.bak
examples/*.tif
examples/*.hdf5
.pytest_cache
2 changes: 2 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ Examples
.. fancy_include:: hyperspy_hologram.py

.. fancy_include:: convert_txt2npy.py

.. fancy_include:: convert_txt2tif.py
2 changes: 1 addition & 1 deletion examples/convert_txt2npy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class for a very unique data set. In this example, QPI data,
This example must be executed with a directory as an
command line argument, i.e.
``python convert_tcxt2tig /path/to/folder/``
``python convert_txt2npy /path/to/folder/``
"""
import pathlib
import sys
Expand Down
58 changes: 58 additions & 0 deletions examples/convert_txt2tif.py
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)

0 comments on commit 4e51a2d

Please sign in to comment.