fabio 0.1.1__zip
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- Library/Python/2.7/site-packages/fabio/GEimage.py +334 -0
- Library/Python/2.7/site-packages/fabio/GEimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/GEimage_old.py +501 -0
- Library/Python/2.7/site-packages/fabio/GEimage_old.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/HiPiCimage.py +106 -0
- Library/Python/2.7/site-packages/fabio/HiPiCimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/OXDimage.py +442 -0
- Library/Python/2.7/site-packages/fabio/OXDimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/TiffIO.py +1197 -0
- Library/Python/2.7/site-packages/fabio/TiffIO.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/__init__.py +10 -0
- Library/Python/2.7/site-packages/fabio/__init__.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/adscimage.py +137 -0
- Library/Python/2.7/site-packages/fabio/adscimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/binaryimage.py +87 -0
- Library/Python/2.7/site-packages/fabio/binaryimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/bruker100image.py +96 -0
- Library/Python/2.7/site-packages/fabio/bruker100image.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/brukerimage.py +195 -0
- Library/Python/2.7/site-packages/fabio/brukerimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/byte_offset.so +0 -0
- Library/Python/2.7/site-packages/fabio/cbfimage.py +758 -0
- Library/Python/2.7/site-packages/fabio/cbfimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/cf_io.so +0 -0
- Library/Python/2.7/site-packages/fabio/compression.py +388 -0
- Library/Python/2.7/site-packages/fabio/compression.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/converters.py +54 -0
- Library/Python/2.7/site-packages/fabio/converters.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/datIO.py +60 -0
- Library/Python/2.7/site-packages/fabio/datIO.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/dm3image.py +219 -0
- Library/Python/2.7/site-packages/fabio/dm3image.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/edfimage.py +924 -0
- Library/Python/2.7/site-packages/fabio/edfimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/fabioimage.py +556 -0
- Library/Python/2.7/site-packages/fabio/fabioimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/fabioutils.py +491 -0
- Library/Python/2.7/site-packages/fabio/fabioutils.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/file_series.py +313 -0
- Library/Python/2.7/site-packages/fabio/file_series.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/fit2dmaskimage.py +94 -0
- Library/Python/2.7/site-packages/fabio/fit2dmaskimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/fit2dspreadsheetimage.py +85 -0
- Library/Python/2.7/site-packages/fabio/fit2dspreadsheetimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/kcdimage.py +131 -0
- Library/Python/2.7/site-packages/fabio/kcdimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/mar345_IO.so +0 -0
- Library/Python/2.7/site-packages/fabio/mar345image.py +320 -0
- Library/Python/2.7/site-packages/fabio/mar345image.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/marccdimage.py +309 -0
- Library/Python/2.7/site-packages/fabio/marccdimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/openimage.py +162 -0
- Library/Python/2.7/site-packages/fabio/openimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/pilatusimage.py +81 -0
- Library/Python/2.7/site-packages/fabio/pilatusimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/pnmimage.py +165 -0
- Library/Python/2.7/site-packages/fabio/pnmimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/readbytestream.py +86 -0
- Library/Python/2.7/site-packages/fabio/readbytestream.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/tifimage.py +283 -0
- Library/Python/2.7/site-packages/fabio/tifimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio/xsdimage.py +134 -0
- Library/Python/2.7/site-packages/fabio/xsdimage.pyc +0 -0
- Library/Python/2.7/site-packages/fabio-0.1.1-py2.7.egg-info/PKG-INFO +11 -0
- Library/Python/2.7/site-packages/fabio-0.1.1-py2.7.egg-info/SOURCES.txt +114 -0
- Library/Python/2.7/site-packages/fabio-0.1.1-py2.7.egg-info/dependency_links.txt +1 -0
- Library/Python/2.7/site-packages/fabio-0.1.1-py2.7.egg-info/top_level.txt +4 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"""
|
|
2
|
+
|
|
3
|
+
Authors: Henning O. Sorensen & Erik Knudsen
|
|
4
|
+
Center for Fundamental Research: Metal Structures in Four Dimensions
|
|
5
|
+
Risoe National Laboratory
|
|
6
|
+
Frederiksborgvej 399
|
|
7
|
+
DK-4000 Roskilde
|
|
8
|
+
email:henning.sorensen@risoe.dk
|
|
9
|
+
|
|
10
|
+
mods for fabio by JPW
|
|
11
|
+
|
|
12
|
+
"""
|
|
13
|
+
import sys, logging
|
|
14
|
+
logger = logging.getLogger("openimage")
|
|
15
|
+
from fabioutils import deconstruct_filename, filename_object
|
|
16
|
+
from fabioimage import fabioimage
|
|
17
|
+
import edfimage
|
|
18
|
+
import adscimage
|
|
19
|
+
import tifimage
|
|
20
|
+
import marccdimage
|
|
21
|
+
import mar345image
|
|
22
|
+
import fit2dmaskimage
|
|
23
|
+
import brukerimage
|
|
24
|
+
import bruker100image
|
|
25
|
+
import pnmimage
|
|
26
|
+
import GEimage
|
|
27
|
+
import OXDimage
|
|
28
|
+
import dm3image
|
|
29
|
+
import HiPiCimage
|
|
30
|
+
import pilatusimage
|
|
31
|
+
import fit2dspreadsheetimage
|
|
32
|
+
import kcdimage
|
|
33
|
+
import cbfimage
|
|
34
|
+
import xsdimage
|
|
35
|
+
import binaryimage
|
|
36
|
+
|
|
37
|
+
MAGIC_NUMBERS = [
|
|
38
|
+
# "\42\5a" : 'bzipped'
|
|
39
|
+
# "\1f\8b" : 'gzipped'
|
|
40
|
+
("FORMAT : 86" , 'bruker'),
|
|
41
|
+
("\x4d\x4d\x00\x2a" , 'tif') ,
|
|
42
|
+
# The marCCD and Pilatus formats are both standard tif with a header
|
|
43
|
+
# hopefully these byte patterns are unique for the formats
|
|
44
|
+
# If not the image will be read, but the is missing
|
|
45
|
+
("\x49\x49\x2a\x00\x08\x00" , 'marccd') ,
|
|
46
|
+
("\x49\x49\x2a\x00\x82\x00" , 'pilatus') ,
|
|
47
|
+
("\x49\x49\x2a\x00" , 'tif') ,
|
|
48
|
+
# ADSC must come before edf
|
|
49
|
+
("{\nHEA" , 'adsc'),
|
|
50
|
+
("{" , 'edf'),
|
|
51
|
+
("\r{" , 'edf'),
|
|
52
|
+
("\n{" , 'edf'),
|
|
53
|
+
("ADEPT" , 'GE'),
|
|
54
|
+
("OD" , 'OXD'),
|
|
55
|
+
("IM" , 'HiPiC'),
|
|
56
|
+
('\x2d\x04' , 'mar345'),
|
|
57
|
+
('\xd2\x04' , 'mar345'),
|
|
58
|
+
('\x04\x2d' , 'mar345'), #some machines may need byteswapping
|
|
59
|
+
('\x04\xd2' , 'mar345'),
|
|
60
|
+
# hint : MASK in 32 bit
|
|
61
|
+
('M\x00\x00\x00A\x00\x00\x00S\x00\x00\x00K\x00\x00\x00' , 'fit2dmask') ,
|
|
62
|
+
('\x00\x00\x00\x03' , 'dm3'),
|
|
63
|
+
("No" , "kcd"),
|
|
64
|
+
("<" , "xsd")
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
def do_magic(byts):
|
|
68
|
+
""" Try to interpret the bytes starting the file as a magic number """
|
|
69
|
+
for magic, format_type in MAGIC_NUMBERS:
|
|
70
|
+
if byts.find(magic) == 0:
|
|
71
|
+
return format_type
|
|
72
|
+
if 0: # debugging - bruker needed 18 bytes below
|
|
73
|
+
logger.debug("m: %s f: %s", magic, format_type)
|
|
74
|
+
logger.debug("bytes: %s len(bytes) %s", magic, len(magic))
|
|
75
|
+
logger.debug("found: %s", byts.find(magic))
|
|
76
|
+
for i in range(len(magic)):
|
|
77
|
+
logger.debug("%s %s %s %s ", ord(magic[i]), ord(byts[i]), magic[i], byts[i])
|
|
78
|
+
raise Exception("Could not interpret magic string")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def openimage(filename, frame=None):
|
|
82
|
+
""" Try to open an image """
|
|
83
|
+
if isinstance(filename, filename_object):
|
|
84
|
+
try:
|
|
85
|
+
logger.debug("Attempting to open %s" % (filename.tostring()))
|
|
86
|
+
obj = _openimage(filename.tostring())
|
|
87
|
+
logger.debug("Attempting to read frame %s from %s" % (frame,
|
|
88
|
+
filename.tostring()))
|
|
89
|
+
obj = obj.read(filename.tostring(), frame)
|
|
90
|
+
except Exception, ex:
|
|
91
|
+
# multiframe file
|
|
92
|
+
#logger.debug( "DEBUG: multiframe file, start # %d"%(
|
|
93
|
+
# filename.num)
|
|
94
|
+
logger.debug("Exception %s, trying name %s" % (ex, filename.stem))
|
|
95
|
+
obj = _openimage(filename.stem)
|
|
96
|
+
logger.debug("Reading frame %s from %s" % (filename.num, filename.stem))
|
|
97
|
+
obj.read(filename.stem, frame=filename.num)
|
|
98
|
+
else:
|
|
99
|
+
logger.debug("Attempting to open %s" % (filename))
|
|
100
|
+
obj = _openimage(filename)
|
|
101
|
+
logger.debug("Attempting to read frame %s from %s" % (frame, filename))
|
|
102
|
+
obj = obj.read(filename, frame)
|
|
103
|
+
return obj
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def openheader(filename):
|
|
107
|
+
""" return only the header"""
|
|
108
|
+
obj = _openimage(filename)
|
|
109
|
+
obj.readheader(filename)
|
|
110
|
+
return obj
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _openimage(filename):
|
|
114
|
+
"""
|
|
115
|
+
determine which format for a filename
|
|
116
|
+
and return appropriate class which can be used for opening the image
|
|
117
|
+
"""
|
|
118
|
+
try:
|
|
119
|
+
imo = fabioimage()
|
|
120
|
+
byts = imo._open(filename).read(18)
|
|
121
|
+
filetype = do_magic(byts)
|
|
122
|
+
if filetype == "marccd" and filename.find("mccd") == -1:
|
|
123
|
+
# Cannot see a way around this. Need to find something
|
|
124
|
+
# to distinguish mccd from regular tif...
|
|
125
|
+
filetype = "tif"
|
|
126
|
+
except IOError, error:
|
|
127
|
+
logger.error("%s: File probably does not exist", error)
|
|
128
|
+
raise error
|
|
129
|
+
except:
|
|
130
|
+
try:
|
|
131
|
+
file_obj = deconstruct_filename(filename)
|
|
132
|
+
if file_obj == None:
|
|
133
|
+
raise Exception("Unable to deconstruct filename")
|
|
134
|
+
if (file_obj.format is not None) and\
|
|
135
|
+
len(file_obj.format) != 1 and \
|
|
136
|
+
type(file_obj.format) != type(["list"]):
|
|
137
|
+
# one of OXD/ ADSC - should have got in previous
|
|
138
|
+
raise Exception("openimage failed on magic bytes & name guess")
|
|
139
|
+
filetype = file_obj.format
|
|
140
|
+
#UNUSED filenumber = file_obj.num
|
|
141
|
+
except Exception, error:
|
|
142
|
+
logger.error(error)
|
|
143
|
+
import traceback
|
|
144
|
+
traceback.print_exc()
|
|
145
|
+
raise Exception("Fabio could not identify " + filename)
|
|
146
|
+
klass_name = "".join(filetype) + 'image'
|
|
147
|
+
module = sys.modules.get("fabio." + klass_name, None)
|
|
148
|
+
if module is not None:
|
|
149
|
+
if hasattr(module, klass_name):
|
|
150
|
+
klass = getattr(module, klass_name)
|
|
151
|
+
else:
|
|
152
|
+
raise Exception("Module %s has no image class" % module)
|
|
153
|
+
else:
|
|
154
|
+
raise Exception("Filetype not known %s %s" % (filename, klass_name))
|
|
155
|
+
obj = klass()
|
|
156
|
+
# skip the read for read header
|
|
157
|
+
return obj
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
Binary file
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
#coding: utf8
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
Authors: Henning O. Sorensen & Erik Knudsen
|
|
6
|
+
Center for Fundamental Research: Metal Structures in Four Dimensions
|
|
7
|
+
Risoe National Laboratory
|
|
8
|
+
Frederiksborgvej 399
|
|
9
|
+
DK-4000 Roskilde
|
|
10
|
+
email:henning.sorensen@risoe.dk
|
|
11
|
+
|
|
12
|
+
+ (mods for fabio) Jon Wright, ESRF
|
|
13
|
+
marccdimage can read MarCCD and MarMosaic images including header info.
|
|
14
|
+
|
|
15
|
+
JPW : Use a parser in case of typos (sorry?)
|
|
16
|
+
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# Base this on the tifimage (as Pilatus is tiff with a
|
|
21
|
+
# tiff header
|
|
22
|
+
|
|
23
|
+
from fabio.tifimage import tifimage
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class pilatusimage(tifimage):
|
|
27
|
+
""" Read in Pilatus format, also
|
|
28
|
+
pilatus images, including header info """
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _readheader(self, infile):
|
|
32
|
+
"""
|
|
33
|
+
Parser based approach
|
|
34
|
+
Gets all entries
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
self.header = {}
|
|
38
|
+
|
|
39
|
+
# infile = open(infile)
|
|
40
|
+
hstr = infile.read(4096)
|
|
41
|
+
# well not very pretty - but seems to find start of
|
|
42
|
+
# header information
|
|
43
|
+
if (hstr.find('# ') == -1):
|
|
44
|
+
return self.header
|
|
45
|
+
|
|
46
|
+
hstr = hstr[hstr.index('# '):]
|
|
47
|
+
hstr = hstr[:hstr.index('\x00')]
|
|
48
|
+
hstr = hstr.split('#')
|
|
49
|
+
go_on = True
|
|
50
|
+
while go_on:
|
|
51
|
+
try:
|
|
52
|
+
hstr.remove('')
|
|
53
|
+
except Exception:
|
|
54
|
+
go_on = False
|
|
55
|
+
|
|
56
|
+
for line in hstr:
|
|
57
|
+
line = line[1:line.index('\r\n')]
|
|
58
|
+
if line.find(':') > -1:
|
|
59
|
+
dump = line.split(':')
|
|
60
|
+
self.header[dump[0]] = dump[1]
|
|
61
|
+
elif line.find('=') > -1:
|
|
62
|
+
dump = line.split('=')
|
|
63
|
+
self.header[dump[0]] = dump[1]
|
|
64
|
+
elif line.find(' ') > -1:
|
|
65
|
+
i = line.find(' ')
|
|
66
|
+
self.header[line[:i]] = line[i:]
|
|
67
|
+
elif line.find(',') > -1:
|
|
68
|
+
dump = line.split(',')
|
|
69
|
+
self.header[dump[0]] = dump[1]
|
|
70
|
+
|
|
71
|
+
return self.header
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _read(self, fname):
|
|
76
|
+
"""
|
|
77
|
+
inherited from tifimage
|
|
78
|
+
... a Pilatus image *is a* tif image
|
|
79
|
+
just with a header
|
|
80
|
+
"""
|
|
81
|
+
return tifimage.read(self, fname)
|
|
Binary file
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
#coding: utf8
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
Authors: Henning O. Sorensen & Erik Knudsen
|
|
6
|
+
Center for Fundamental Research: Metal Structures in Four Dimensions
|
|
7
|
+
Risoe National Laboratory
|
|
8
|
+
Frederiksborgvej 399
|
|
9
|
+
DK-4000 Roskilde
|
|
10
|
+
email:henning.sorensen@risoe.dk
|
|
11
|
+
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import numpy, logging
|
|
15
|
+
logger = logging.getLogger("pnmimage")
|
|
16
|
+
from fabioimage import fabioimage
|
|
17
|
+
|
|
18
|
+
SUBFORMATS = ['P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7']
|
|
19
|
+
|
|
20
|
+
HEADERITEMS = ['SUBFORMAT', 'DIMENSIONS', 'MAXVAL']
|
|
21
|
+
P7HEADERITEMS = ['WIDTH', 'HEIGHT', 'DEPTH', 'MAXVAL', 'TUPLTYPE', 'ENDHDR']
|
|
22
|
+
|
|
23
|
+
class pnmimage(fabioimage):
|
|
24
|
+
def __init__(self, *arg, **kwargs):
|
|
25
|
+
fabioimage.__init__(self, *arg, **kwargs)
|
|
26
|
+
fun = getattr(fabioimage, '__init__', lambda x: None)
|
|
27
|
+
fun(self)
|
|
28
|
+
self.data = None
|
|
29
|
+
self.header = {'Subformat':'P5'}
|
|
30
|
+
self.dim1 = self.dim2 = 0
|
|
31
|
+
self.m = self.maxval = self.stddev = self.minval = None
|
|
32
|
+
self.header_keys = self.header.keys()
|
|
33
|
+
self.bytecode = None
|
|
34
|
+
|
|
35
|
+
def _readheader(self, f):
|
|
36
|
+
#pnm images have a 3-line header but ignore lines starting with '#'
|
|
37
|
+
#1st line contains the pnm image sub format
|
|
38
|
+
#2nd line contains the image pixel dimension
|
|
39
|
+
#3rd line contains the maximum pixel value (at least for grayscale - check this)
|
|
40
|
+
self.header_keys = ['SUBFORMAT', 'DIMENSIONS', 'MAXVAL']
|
|
41
|
+
|
|
42
|
+
l = f.readline().strip()
|
|
43
|
+
if l not in SUBFORMATS:
|
|
44
|
+
raise IOError, ('unknown subformat of pnm: %s' % l)
|
|
45
|
+
else:
|
|
46
|
+
self.header['SUBFORMAT'] = l
|
|
47
|
+
|
|
48
|
+
if self.header['SUBFORMAT'] == 'P7':
|
|
49
|
+
self.header_keys = P7HEADERITEMS
|
|
50
|
+
#this one has a special header
|
|
51
|
+
while 'ENDHDR' not in l:
|
|
52
|
+
l = f.readline()
|
|
53
|
+
while(l[0] == '#'): l = f.readline()
|
|
54
|
+
s = l.lsplit(' ', 1)
|
|
55
|
+
if s[0] not in P7HEADERITEMS:
|
|
56
|
+
raise IOError, ('Illegal pam (netpnm p7) headeritem %s' % s[0])
|
|
57
|
+
self.header[s[0]] = s[1]
|
|
58
|
+
else:
|
|
59
|
+
self.header_keys = HEADERITEMS
|
|
60
|
+
for k in self.header_keys[1:]:
|
|
61
|
+
l = f.readline()
|
|
62
|
+
while(l[0] == '#'): l = f.readline()
|
|
63
|
+
self.header[k] = l.strip()
|
|
64
|
+
|
|
65
|
+
#set the dimensions
|
|
66
|
+
dims = (self.header['DIMENSIONS'].split())
|
|
67
|
+
self.dim1, self.dim2 = int(dims[0]), int(dims[1])
|
|
68
|
+
#figure out how many bytes are used to store the data
|
|
69
|
+
#case construct here!
|
|
70
|
+
m = int(self.header['MAXVAL'])
|
|
71
|
+
if m < 256:
|
|
72
|
+
self.bytecode = numpy.uint8
|
|
73
|
+
elif m < 65536:
|
|
74
|
+
self.bytecode = numpy.uint16
|
|
75
|
+
elif m < 2147483648L:
|
|
76
|
+
self.bytecode = numpy.uint32
|
|
77
|
+
logger.warning('32-bit pixels are not really supported by the netpgm standard')
|
|
78
|
+
else:
|
|
79
|
+
raise IOError, 'could not figure out what kind of pixels you have'
|
|
80
|
+
|
|
81
|
+
def read(self, fname, frame=None):
|
|
82
|
+
"""
|
|
83
|
+
try to read PNM images
|
|
84
|
+
@param fname: name of the file
|
|
85
|
+
@param frame: not relevant here! PNM is always single framed
|
|
86
|
+
"""
|
|
87
|
+
self.header = {}
|
|
88
|
+
self.resetvals()
|
|
89
|
+
infile = self._open(fname)
|
|
90
|
+
self._readheader(infile)
|
|
91
|
+
|
|
92
|
+
#read the image data
|
|
93
|
+
decoder_name = "%sdec" % self.header['SUBFORMAT']
|
|
94
|
+
if decoder_name in dir(pnmimage):
|
|
95
|
+
decoder = getattr(pnmimage, decoder_name)
|
|
96
|
+
self.data = decoder(infile, self.bytecode)
|
|
97
|
+
else:
|
|
98
|
+
raise IOError("No decoder named %s for file %s" % (decoder_name, fname))
|
|
99
|
+
self.resetvals()
|
|
100
|
+
return self
|
|
101
|
+
|
|
102
|
+
@staticmethod
|
|
103
|
+
def P1dec(buf, bytecode):
|
|
104
|
+
data = numpy.zeros((self.dim2, self.dim1))
|
|
105
|
+
i = 0
|
|
106
|
+
for l in buf.readlines():
|
|
107
|
+
try:
|
|
108
|
+
data[i, :] = numpy.array(l.split()).astype(bytecode)
|
|
109
|
+
except ValueError:
|
|
110
|
+
raise IOError, 'Size spec in pnm-header does not match size of image data field'
|
|
111
|
+
return data
|
|
112
|
+
|
|
113
|
+
@staticmethod
|
|
114
|
+
def P4dec(buf, bytecode):
|
|
115
|
+
err = 'single bit (pbm) images are not supported - yet'
|
|
116
|
+
logger.error(err)
|
|
117
|
+
raise NotImplementedError(err)
|
|
118
|
+
|
|
119
|
+
@staticmethod
|
|
120
|
+
def P2dec(buf, bytecode):
|
|
121
|
+
data = numpy.zeros((self.dim2, self.dim1))
|
|
122
|
+
i = 0
|
|
123
|
+
for l in buf.readlines():
|
|
124
|
+
try:
|
|
125
|
+
data[i, :] = numpy.array(l.split()).astype(bytecode)
|
|
126
|
+
except ValueError:
|
|
127
|
+
raise IOError, 'Size spec in pnm-header does not match size of image data field'
|
|
128
|
+
return data
|
|
129
|
+
|
|
130
|
+
@staticmethod
|
|
131
|
+
def P5dec(buf, bytecode):
|
|
132
|
+
l = buf.read()
|
|
133
|
+
try:
|
|
134
|
+
data = numpy.reshape(numpy.fromstring(l, bytecode), [self.dim2, self.dim1]).byteswap()
|
|
135
|
+
except ValueError:
|
|
136
|
+
raise IOError, 'Size spec in pnm-header does not match size of image data field'
|
|
137
|
+
return data
|
|
138
|
+
|
|
139
|
+
@staticmethod
|
|
140
|
+
def P3dec(buf, bytecode):
|
|
141
|
+
err = '(plain-ppm) RGB images are not supported - yet'
|
|
142
|
+
logger.error(err)
|
|
143
|
+
raise NotImplementedError(err)
|
|
144
|
+
|
|
145
|
+
@staticmethod
|
|
146
|
+
def P6dec(buf, bytecode):
|
|
147
|
+
err = '(ppm) RGB images are not supported - yet'
|
|
148
|
+
logger.error(err)
|
|
149
|
+
raise NotImplementedError(err)
|
|
150
|
+
|
|
151
|
+
@staticmethod
|
|
152
|
+
def P7dec(buf, bytecode):
|
|
153
|
+
err = '(pam) images are not supported - yet'
|
|
154
|
+
logger.error(err)
|
|
155
|
+
raise NotImplementedError(err)
|
|
156
|
+
|
|
157
|
+
def write(self, filename):
|
|
158
|
+
raise NotImplementedError('write pnm images is not implemented yet.')
|
|
159
|
+
|
|
160
|
+
@staticmethod
|
|
161
|
+
def checkData(data=None):
|
|
162
|
+
if data is None:
|
|
163
|
+
return None
|
|
164
|
+
else:
|
|
165
|
+
return data.astype(int)
|
|
Binary file
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
#coding: utf8
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Reads a bytestream
|
|
6
|
+
|
|
7
|
+
Authors: Jon Wright Henning O. Sorensen & Erik Knudsen
|
|
8
|
+
ESRF Risoe National Laboratory
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import numpy, logging
|
|
12
|
+
logger = logging.getLogger("readbytestream")
|
|
13
|
+
DATATYPES = {
|
|
14
|
+
# type sign bytes
|
|
15
|
+
("int", 'n', 1) : numpy.uint8,
|
|
16
|
+
("int", 'n', 2) : numpy.uint16,
|
|
17
|
+
("int", 'n', 4) : numpy.uint32,
|
|
18
|
+
("int", 'y', 1) : numpy.int8,
|
|
19
|
+
("int", 'y', 2) : numpy.int16,
|
|
20
|
+
("int", 'y', 4) : numpy.int32,
|
|
21
|
+
('float', 'y', 4) : numpy.float32, # does this occur in bruker?
|
|
22
|
+
('double', 'y', 4): numpy.float64
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def readbytestream(fil,
|
|
27
|
+
offset,
|
|
28
|
+
x,
|
|
29
|
+
y,
|
|
30
|
+
nbytespp,
|
|
31
|
+
datatype='int',
|
|
32
|
+
signed='n',
|
|
33
|
+
swap='n',
|
|
34
|
+
typeout=numpy.uint16):
|
|
35
|
+
"""
|
|
36
|
+
Reads in a bytestream from a file (which may be a string indicating
|
|
37
|
+
a filename, or an already opened file (should be "rb"))
|
|
38
|
+
offset is the position (in bytes) where the pixel data start
|
|
39
|
+
nbytespp = number of bytes per pixel
|
|
40
|
+
type can be int or float (4 bytes pp) or double (8 bytes pp)
|
|
41
|
+
signed: normally signed data 'y', but 'n' to try to get back the
|
|
42
|
+
right numbers when unsigned data are converted to signed
|
|
43
|
+
(python once had no unsigned numeric types.)
|
|
44
|
+
swap, normally do not bother, but 'y' to swap bytes
|
|
45
|
+
typeout is the numpy type to output, normally uint16,
|
|
46
|
+
but more if overflows occurred
|
|
47
|
+
x and y are the pixel dimensions
|
|
48
|
+
|
|
49
|
+
TODO : Read in regions of interest
|
|
50
|
+
|
|
51
|
+
PLEASE LEAVE THE STRANGE INTERFACE ALONE -
|
|
52
|
+
IT IS USEFUL FOR THE BRUKER FORMAT
|
|
53
|
+
"""
|
|
54
|
+
tin = "dunno"
|
|
55
|
+
length = nbytespp * x * y # bytes per pixel times number of pixels
|
|
56
|
+
if datatype in ['float', 'double']:
|
|
57
|
+
signed = 'y'
|
|
58
|
+
|
|
59
|
+
key = (datatype, signed, nbytespp)
|
|
60
|
+
try:
|
|
61
|
+
tin = DATATYPES[key]
|
|
62
|
+
except:
|
|
63
|
+
logging.warning("datatype,signed,nbytespp " + str(key))
|
|
64
|
+
raise Exception("Unknown combination of types to readbytestream")
|
|
65
|
+
|
|
66
|
+
# Did we get a string (filename) or a readable stream object?
|
|
67
|
+
if hasattr(fil, "read") and hasattr(fil, "seek"):
|
|
68
|
+
infile = fil
|
|
69
|
+
opened = False
|
|
70
|
+
else:
|
|
71
|
+
infile = open(fil, 'rb')
|
|
72
|
+
opened = True
|
|
73
|
+
|
|
74
|
+
infile.seek(offset)
|
|
75
|
+
|
|
76
|
+
arr = numpy.array(numpy.reshape(
|
|
77
|
+
numpy.fromstring(
|
|
78
|
+
infile.read(length), tin) , (x, y)), typeout)
|
|
79
|
+
|
|
80
|
+
if swap == 'y':
|
|
81
|
+
arr = arr.byteswap()
|
|
82
|
+
|
|
83
|
+
if opened:
|
|
84
|
+
infile.close()
|
|
85
|
+
|
|
86
|
+
return arr
|
|
Binary file
|