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

Source Code for Module fabio.openimage

  1   
  2   
  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 by JPW 
 13   
 14  """ 
 15   
 16  import fabio 
 17  from fabio import deconstruct_filename, getnum 
 18  from fabio.fabioimage import fabioimage 
 19   
 20   
 21  from fabio import edfimage 
 22  from fabio import adscimage 
 23  from fabio import tifimage 
 24  from fabio import marccdimage 
 25  from fabio import mar345image 
 26  from fabio import fit2dmaskimage 
 27  from fabio import brukerimage 
 28  from fabio import bruker100image 
 29  from fabio import pnmimage 
 30  from fabio import GEimage 
 31  from fabio import OXDimage 
 32  from fabio import dm3image 
 33  from fabio import HiPiCimage 
 34  from fabio import pilatusimage 
 35  from fabio import fit2dspreadsheetimage 
 36  from fabio import kcdimage 
 37  from fabio import cbfimage 
 38   
 39   
 40  MAGIC_NUMBERS = [ 
 41      # "\42\5a" : 'bzipped' 
 42      # "\1f\8b" : 'gzipped' 
 43      ("FORMAT :        86" , 'bruker'), 
 44      ("\x4d\x4d\x00\x2a"   , 'tif') , 
 45      # The marCCD and Pilatus formats are both standard tif with a header 
 46      # hopefully these byte patterns are unique for the formats 
 47      # If not the image will be read, but the is missing  
 48      ("\x49\x49\x2a\x00\x08\x00"   , 'marccd') , 
 49      ("\x49\x49\x2a\x00\x82\x00"   , 'pilatus') , 
 50      ("\x49\x49\x2a\x00"   , 'tif') , 
 51      # ADSC must come before edf 
 52      ("{\nHEA"             , 'adsc'), 
 53      ("{"                  , 'edf'), 
 54      ("\r{"                , 'edf'), 
 55      ("\n{"                , 'edf'), 
 56      ("ADEPT"              , 'GE'), 
 57      ("OD"                 , 'OXD'), 
 58      ("IM"                 , 'HiPiC'), 
 59      ('\x2d\x04'           , 'mar345'), 
 60      ('\x04\x2d'           , 'mar345'), #some machines may need byteswapping 
 61      # hint : MASK in 32 bit 
 62      ('M\x00\x00\x00A\x00\x00\x00S\x00\x00\x00K\x00\x00\x00' , 'fit2dmask') , 
 63      ('\x00\x00\x00\x03'   , 'dm3'), 
 64      ("No"                 , "kcd") 
 65      ] 
 66   
67 -def do_magic(byts):
68 """ Try to interpret the bytes starting the file as a magic number """ 69 for magic, format in MAGIC_NUMBERS: 70 if byts.find(magic) == 0: 71 return format 72 if 0: # debugging - bruker needed 18 bytes below 73 print "m:", magic, "f:", format, 74 print "bytes:", magic, "len(bytes)", len(magic), 75 print "found:", byts.find(magic) 76 for i in range(len(magic)): 77 print ord(magic[i]), ord(byts[i]), magic[i], byts[i] 78 raise Exception("Could not interpret magic string")
79 80
81 -def openimage(filename):
82 """ Try to open an image """ 83 import fabio 84 if isinstance(filename, fabio.filename_object): 85 try: 86 obj = _openimage(filename.tostring()) 87 obj.read(filename.tostring()) 88 except: 89 # multiframe file 90 #print "DEBUG: multiframe file, start # %d"%( 91 # filename.num) 92 obj = _openimage(filename.stem) 93 obj.read(filename.stem, frame=filename.num) 94 else: 95 obj = _openimage(filename) 96 obj.read(filename) 97 return obj
98 99
100 -def openheader(filename):
101 """ return only the header""" 102 obj = _openimage(filename) 103 obj.readheader(filename) 104 return obj
105 106
107 -def _openimage(filename):
108 """ 109 determine which format for a filename 110 and return appropriate class which can be used for opening the image 111 """ 112 try: 113 imo = fabioimage() 114 byts = imo._open(filename).read(18) 115 filetype = do_magic(byts) 116 # print filetype 117 if filetype == "marccd" and filename.find("mccd") == -1: 118 # Cannot see a way around this. Need to find something 119 # to distinguish mccd from regular tif... 120 filetype = "tif" 121 #UNUSED filenumber = getnum(filename) 122 except IOError: 123 # File probably does not exist 124 raise 125 except: 126 try: 127 file_obj = deconstruct_filename(filename) 128 if file_obj == None: 129 raise Exception 130 if len(file_obj.format) != 1 and \ 131 type(file_obj.format) != type(["list"]): 132 # one of OXD/ ADSC - should have got in previous 133 raise Exception("openimage failed on magic bytes & name guess") 134 filetype = file_obj.format 135 #UNUSED filenumber = file_obj.num 136 except: 137 #import traceback 138 #traceback.print_exc() 139 raise Exception("Fabio could not identify " + filename) 140 klass_name = "".join(filetype) + 'image' 141 # print "looking for",klass_name 142 if hasattr(fabio, klass_name): 143 module = getattr(fabio, klass_name) 144 if hasattr(module, klass_name): 145 klass = getattr(module, klass_name) 146 # print klass 147 else: 148 raise Exception("Module " + module + "has no image class") 149 else: 150 raise Exception("Filetype not known " + filename + " " + 151 klass_name) 152 obj = klass() 153 # skip the read for read header 154 return obj
155