Files
TOMCAT-DMP-Files/Python/DMPFile.py
T
2015-02-02 15:34:36 +01:00

45 lines
1.1 KiB
Python

import numpy as np
def readDMPFile(fileName):
'''
Opens and reads DMP file. Returns numpy ndarray.
'''
fd = open(fileName, 'rb')
#datatype is unsigned short
datatype = 'h'
numberOfHeaderValues = 3
headerData = np.fromfile(fd, datatype, numberOfHeaderValues)
imageShape = (headerData[1], headerData[0])
imageData = np.fromfile(fd, np.float32, -1)
imageData = imageData.reshape(imageShape)
return imageData
def writeDMPFile(fileName, np_array):
'''
Writes numpy ndarray to DMP file.
ndArray.dtype should be np.float32. If it not, it will be converted before saving.
'''
# opens file for writing (binary)
fd = open(fileName, 'wb')
# create header - must be dtype np.uint16 !!!
width = np.array(np_array.shape[1])
height = np.array(np_array.shape[0])
header = np.array([width, height, 0], np.uint16)
header.tofile(fd)
# img data/numpy ndarray
# dtype has to be np.float32
if np_array.dtype != 'float32':
np_array = np_array.astype(np.float32)
np_array.tofile(fd)