Package fabio :: Module bruker100image
[hide private]
[frames] | no frames]

Source Code for Module fabio.bruker100image

 1   
 2  import numpy as N 
 3  import math 
 4  import Image 
 5  from brukerimage import brukerimage 
 6  import readbytestream 
 7   
8 -class bruker100image(brukerimage):
9 10
11 - def toPIL16(self, filename=None):
12 # FIXME - why is this different for bruker100images? 13 if filename: 14 self.read(filename) 15 PILimage = Image.frombuffer("F", 16 (self.dim1, self.dim2), 17 self.data, 18 "raw", 19 "F;16", 0, -1) 20 return PILimage 21 else: 22 PILimage = Image.frombuffer("F", 23 (self.dim1, self.dim2), 24 self.data, 25 "raw", 26 "F;16", 0, -1) 27 return PILimage
28
29 - def read(self, fname):
30 f = open(fname, "rb") 31 try: 32 self._readheader(f) 33 except: 34 raise 35 36 rows = int(self.header['NROWS']) 37 cols = int(self.header['NCOLS']) 38 npixelb = int(self.header['NPIXELB'][0]) 39 # you had to read the Bruker docs to know this! 40 41 # We are now at the start of the image - assuming 42 # readbrukerheader worked 43 # size = rows * cols * npixelb 44 self.data = readbytestream(f, f.tell(), rows, cols, npixelb, 45 datatype="int", signed='n', swap='n') 46 47 noverfl = self.header['NOVERFL'].split() # now process the overflows 48 #read the set of "underflow pixels" - these will be completely 49 # disregarded for now 50 data = self.data 51 k = 0 52 53 while k < 2:#for the time being things - are done in 16 bits 54 datatype = {'1' : N.uint8, 55 '2' : N.uint16, 56 '4' : N.uint32 }[("%d" % 2 ** k)] 57 ar = N.array(N.fromstring(f.read(int(noverfl[k]) * (2 ** k)), 58 datatype), N.uint16) 59 #insert the the overflow pixels in the image array: 60 #this is probably a memory intensive way of doing this - 61 # might be done in a more clever way 62 lim = 2 ** (8 * k) - 1 63 #generate an array comprising of the indices into data.ravel() 64 # where its value equals lim. 65 M = N.compress(N.equal(data.ravel(), lim), N.arange(rows * cols)) 66 #now put values from ar into those indices 67 N.put(data.ravel(), M, ar) 68 padding = 16 * int(math.ceil(int(noverfl[k]) * (2 ** k) / 16.)) - \ 69 int(noverfl[k]) * (2 ** k) 70 f.seek(padding, 1) 71 print noverfl[k] + " bytes read + %d bytes padding" % padding 72 k = k + 1 73 74 f.close() 75 76 (self.dim1, self.dim2) = (rows, cols) 77 print self.dim1, self.dim2 78 self.resetvals() 79 return self
80 81 if __name__ == '__main__': 82 import sys, time 83 I = bruker100image() 84 b = time.clock() 85 while (sys.argv[1:]): 86 I.read(sys.argv[1]) 87 r = I.toPIL16() 88 I.rebin(2, 2) 89 print sys.argv[1] + (": max=%d, min=%d, mean=%.2e, stddev=%.2e") % ( 90 I.getmax(), I.getmin(), I.getmean(), I.getstddev()) 91 print 'integrated intensity (%d %d %d %d) =%.3f' % ( 92 10, 20, 20, 40, I.integrate_area((10, 20, 20, 40))) 93 sys.argv[1:] = sys.argv[2:] 94 e = time.clock() 95 print (e - b) 96