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

Source Code for Module fabio.binaryimage

 1  #!/usr/bin/env python 
 2  #coding: utf8  
 3  from __future__ import with_statement 
 4  __doc__ = """ 
 5  Authors: Gael Goret, Jerome Kieffer, ESRF, France 
 6  Emails: gael.goret@esrf.fr, jerome.kieffer@esrf.fr 
 7   
 8  Binary files images are simple none-compressed 2D images only defined by their :  
 9  data-type, dimensions, byte order and offset 
10   
11  This simple library has been made for manipulating exotic/unknown files format.   
12  """ 
13   
14  __authors__ = ["Gaël Goret", "Jérôme Kieffer"] 
15  __contact__ = "gael.goret@esrf.fr"#, jerome.kieffer@esrf.eu" 
16  __license__ = "GPLv3+" 
17  __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" 
18  __version__ = "17 Apr 2012" 
19   
20  from fabioimage import fabioimage 
21  import numpy, logging 
22  logger = logging.getLogger("binaryimage") 
23 24 -class binaryimage(fabioimage):
25 """ 26 This simple library has been made for manipulating exotic/unknown files format. 27 28 Binary files images are simple none-compressed 2D images only defined by their : 29 data-type, dimensions, byte order and offset 30 """ 31
32 - def __init__(self, *args, **kwargs):
33 fabioimage.__init__(self, *args, **kwargs)
34 35 @staticmethod
36 - def swap_needed(endian):
37 """ 38 Decide if we need to byteswap 39 """ 40 if (endian == '<' and numpy.little_endian) or (endian == '>' and not numpy.little_endian): 41 return False 42 if (endian == '>' and numpy.little_endian) or (endian == '<' and not numpy.little_endian): 43 return True
44
45 - def read(self, fname, dim1, dim2, offset=0, bytecode="int32", endian="<"):
46 """ 47 Read a binary image 48 Parameters : fname, dim1, dim2, offset, bytecode, endian 49 fname : file name : str 50 dim1,dim2 : image dimensions : int 51 offset : size of the : int 52 bytecode among : "int8","int16","int32","int64","uint8","uint16","uint32","uint64","float32","float64",... 53 endian among short or long endian ("<" or ">") 54 """ 55 self.filename = fname 56 self.dim1 = dim1 57 self.dim2 = dim2 58 self.bytecode = bytecode 59 f = open(self.filename, "rb") 60 dims = [dim2, dim1] 61 bpp = len(numpy.array(0, bytecode).tostring()) 62 size = dims[0] * dims[1] * bpp 63 64 f.seek(offset) 65 rawData = f.read(size) 66 if self.swap_needed(endian): 67 data = numpy.fromstring(rawData, bytecode).byteswap().reshape(tuple(dims)) 68 else: 69 data = numpy.fromstring(rawData, bytecode).reshape(tuple(dims)) 70 self.data = data 71 return self
72
73 - def estimate_offset_value(self, fname, dim1, dim2, bytecode="int32"):
74 "Estimates the size of a file" 75 with open(fname, "rb") as f: 76 bpp = len(numpy.array(0, bytecode).tostring()) 77 size = dim1 * dim2 * bpp 78 totsize = len(f.read()) 79 logger.info('total size (bytes): %s', totsize) 80 logger.info('expected data size given parameters (bytes): %s', size) 81 logger.info('estimation of the offset value (bytes): %s', totsize - size)
82
83 - def write(self, fname):
84 with open(fname, mode="wb") as outfile: 85 outfile.write(self.data.tostring())
86