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

Source Code for Module fabio.HiPiCimage

  1   
  2  #!/usr/bin/env python 
  3  """ 
  4  Authors: Henning O. Sorensen & Erik Knudsen 
  5           Center for Fundamental Research: Metal Structures in Four Dimensions 
  6           Risoe National Laboratory 
  7           Frederiksborgvej 399 
  8           DK-4000 Roskilde 
  9           email:erik.knudsen@risoe.dk 
 10   
 11          + Jon Wright, ESRF 
 12   
 13  Information about the file format from Masakatzu Kobayashi is highly appreciated 
 14  """ 
 15   
 16  import numpy as np, logging 
 17   
 18  from fabioimage import fabioimage 
 19   
20 -class HiPiCimage(fabioimage):
21 """ Read HiPic images e.g. collected with a Hamamatsu CCD camera""" 22 23
24 - def _readheader(self, infile):
25 """ 26 Read in a header from an already open file 27 28 """ 29 Image_tag = infile.read(2) 30 print Image_tag 31 Comment_len = np.fromstring(infile.read(2), np.uint16) 32 Dim_1 = np.fromstring(infile.read(2), np.uint16)[0] 33 Dim_2 = np.fromstring(infile.read(2), np.uint16)[0] 34 Dim_1_offset = np.fromstring(infile.read(2), np.uint16)[0] 35 Dim_2_offset = np.fromstring(infile.read(2), np.uint16)[0] 36 HeaderType = np.fromstring(infile.read(2), np.uint16)[0] 37 Dump = infile.read(50) 38 Comment = infile.read(Comment_len) 39 self.header['Image_tag'] = Image_tag 40 self.header['Dim_1'] = Dim_1 41 self.header['Dim_2'] = Dim_2 42 self.header['Dim_1_offset'] = Dim_1_offset 43 self.header['Dim_2_offset'] = Dim_2_offset 44 #self.header['Comment'] = Comment 45 if Image_tag != 'IM' : 46 # This does not look like an HiPic file 47 logging.warning("no opening. Corrupt header of HiPic file " + \ 48 str(infile.name)) 49 Comment_split = Comment[:Comment.find('\x00')].split('\r\n') 50 51 for topcomment in Comment_split: 52 topsplit = topcomment.split(',') 53 for line in topsplit: 54 if '=' in line: 55 key, val = line.split('=' , 1) 56 # Users cannot type in significant whitespace 57 key = key.rstrip().lstrip() 58 self.header_keys.append(key) 59 self.header[key] = val.lstrip().rstrip() 60 self.header[key] = val.lstrip('"').rstrip('"')
61
62 - def read(self, fname):
63 """ 64 Read in header into self.header and 65 the data into self.data 66 """ 67 self.header = {} 68 self.resetvals() 69 infile = self._open(fname, "rb") 70 self._readheader(infile) 71 # Compute image size 72 try: 73 self.dim1 = int(self.header['Dim_1']) 74 self.dim2 = int(self.header['Dim_2']) 75 except: 76 raise Exception("HiPic file", str(fname) + \ 77 "is corrupt, cannot read it") 78 bytecode = np.uint16 79 self.bpp = len(np.array(0, bytecode).tostring()) 80 81 # Read image data 82 block = infile.read(self.dim1 * self.dim2 * self.bpp) 83 infile.close() 84 85 #now read the data into the array 86 try: 87 self.data = np.reshape( 88 np.fromstring(block, bytecode), 89 [self.dim2, self.dim1]) 90 except: 91 print len(block), bytecode, self.bpp, self.dim2, self.dim1 92 raise IOError, \ 93 'Size spec in HiPic-header does not match size of image data field' 94 self.bytecode = self.data.dtype.type 95 96 # Sometimes these files are not saved as 12 bit, 97 # But as 16 bit after bg subtraction - which results 98 # negative values saved as 16bit. Therefore values higher 99 # 4095 is really negative values 100 if self.data.max() > 4095: 101 gt12bit = self.data > 4095 102 self.data = self.data - gt12bit * (2 ** 16 - 1) 103 104 # ensure the PIL image is reset 105 self.pilimage = None 106 return self
107