Skip to content

Commit e75ef35

Browse files
committed
Load byte order correctly
1 parent a7da986 commit e75ef35

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

mhd_utils/__init__.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ def load_raw_data_with_mhd(filename: PathLike) -> Tuple[np.ndarray, Dict[str, An
103103
data = np.fromfile(f, count=np.prod(shape), dtype=np_type)
104104
data.shape = shape
105105

106+
# Adjust byte order in numpy array to match default system byte order
107+
if 'BinaryDataByteOrderMSB' in meta_dict:
108+
sys_byteorder_msb = sys.byteorder == 'big'
109+
file_byteorder_ms = meta_dict['BinaryDataByteOrderMSB']
110+
if sys_byteorder_msb != file_byteorder_ms:
111+
data = data.byteswap()
112+
106113
# Begin 3D fix
107114
arr.reverse()
108115
if element_channels > 1:
@@ -139,7 +146,8 @@ def write_meta_header(filename: PathLike, meta_dict: Dict[str, Any]):
139146

140147
def write_mhd_file(filename: PathLike, data: np.ndarray, **meta_dict):
141148
"""
142-
Write a meta file and the raw file
149+
Write a meta file and the raw file.
150+
The byte order of the raw file will always be in the byte order of the system.
143151
144152
:param filename: file to write
145153
:param meta_dict: dictionary of meta data in MetaImage format

0 commit comments

Comments
 (0)