Updated Python example

The Python example now features reading and displaying an example DMP
file.
The example.DMP file will be added to the repository once I get the
clearance from the original user.
Additionally we load a "random" image from the web and save that as DMP
to show how to save images as DMP.
Also renamed script :)
This commit is contained in:
David Haberthür
2015-02-02 17:21:01 +01:00
parent d5f0bdb22b
commit b7ed22991c
2 changed files with 74 additions and 44 deletions
-44
View File
@@ -1,44 +0,0 @@
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)
+74
View File
@@ -0,0 +1,74 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Python script to read DMP file.
Written by David Haberthür <david.haberthuer@psi.ch> based on an initial
version by Martin Nyvlt <martin.nyvlt@gmail.com>
"""
import numpy
import os
import matplotlib.pyplot as plt
import urllib2
def readDMP(filename):
"""
Opens and reads DMP file.
Returns a numpy ndarray which can be displayed/written/handled further.
"""
# Open the file
dmpfile = open(filename, 'rb')
# The DMP file format is a very simple file format; the header consists of
# 3 unsigned 16bit integers specifying the image dimensions, followed by a
# stream of 32-bit floats in little-endian byte order.
# Load Header. It's first two values are the image size.
header = numpy.fromfile(dmpfile, numpy.int16, 3)
imageSize = (header[1], header[0])
# Load file and reshape to the size from the header
image = numpy.fromfile(dmpfile, numpy.float32)
image = image.reshape(imageSize)
dmpfile.close()
return image
def writeDMP(array, outputfilename):
"""
Writes numpy ndarray to DMP file.
The array.dtype should be numpy.float32.
If it's not, it will be converted before saving.
"""
# Open the file for writing (binary)
dmpfile = open(outputfilename, 'wb')
# Create header. *Must* be dtype numpy.uint16!
width = numpy.array(array.shape[1])
height = numpy.array(array.shape[0])
header = numpy.array([width, height, 0], numpy.uint16)
header.tofile(dmpfile)
# Write Image data. dtype has to be np.float32
if array.dtype != 'float32':
array = array.astype(numpy.float32)
array.tofile(dmpfile)
dmpfile.close()
# Read example DMP file with the function defined above
DMPFileName = os.path.join(os.getcwd(), '..', 'example.DMP')
DMP = readDMP(DMPFileName)
# Show example image
plt.subplot(121)
plt.imshow(DMP, cmap='gray')
plt.title('example.DMP')
# Load some image from the internet and save its green channel as DMP.
URL = urllib2.urlopen('http://www.psi.ch/sls/tomcat/HomeEN/tomcat_banner.png')
image = plt.imread(URL)[:, :, 1]
writeDMP(image, 'randomimage.DMP')
# Read the image again as DMP and display it
plt.subplot(122)
plt.imshow(readDMP('randomimage.DMP'), cmap='gray')
plt.title('Some random image')
plt.show()
print 'Now open randomimage.DMP in Fiji!'