Commit ab93eeca authored by Pierre Paleo's avatar Pierre Paleo
Browse files

Add EDFWriter

parent 5b6ef9fb
Pipeline #39370 passed with stages
in 5 minutes and 4 seconds
......@@ -11,6 +11,10 @@ from ..misc.utils import rescale_data
from .config import export_dict_to_h5
from .utils import check_h5py_version # won't be necessary once h5py >= 3.0 required
try:
from silx.third_party.EdfFile import EdfFile
except ImportError:
EdfFile = None
try:
from glymur import Jp2k
__have_jp2k__ = True
......@@ -282,6 +286,57 @@ class TIFFWriter(Writer):
return path.dirname(self.fname)
class EDFWriter(Writer):
def __init__(self, fname, start_index=0, filemode="w"):
"""
EDF (ESRF Data Format) writer.
Parameters
-----------
fname: str
Path to the output file name
start_index: int, optional
When writing a stack of images, each image is written in a dedicated file
In this case, the output is a series of files `filename_0000.tif`,
`filename_0001.edf`, etc. This parameter is the starting index for
file names.
"""
super().__init__(fname)
self.filemode = filemode
self.start_index = start_index
def _write_edf(self, data, config=None, filename=None, filemode=None):
if filename is None:
filename = self.fname
edf = EdfFile(filename)
config = config or {}
config["software"] = str("nabu %s" % version)
config["date"] = get_datetime()
edf.WriteImage(
config,
data
)
edf = None
def write(self, data, *args, config=None, **kwargs):
if data.ndim < 3:
self._write_edf(data, config=config)
return
if (data.ndim == 3) and (data.shape[0] == 1):
self._write_edf(data[0], config=config)
return
dirname, rel_filename = path.split(self.fname)
prefix, ext = path.splitext(rel_filename)
for i in range(data.shape[0]):
curr_rel_filename = prefix + str("_%04d" % (self.start_index + i)) + ext
fname = path.join(dirname, curr_rel_filename)
self._write_edf(data[i], filename=fname, config=config)
def get_filename(self):
return path.dirname(self.fname)
class JP2Writer(Writer):
def __init__(self, fname, start_index=0, filemode="wb", psnr=None, auto_convert=True):
"""
......@@ -379,5 +434,6 @@ Writers = {
"j2k": JP2Writer,
"jp2": JP2Writer,
"jp2k": JP2Writer,
"edf": EDFWriter,
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment