PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/dependencies/pymic/zvi/zviread.py

https://bitbucket.org/odebeir/p2012-cellmotion
Python | 209 lines | 196 code | 0 blank | 13 comment | 0 complexity | 69feac371d4c414f70e0454eb26c2b7a MD5 | raw file
Possible License(s): Apache-2.0
  1. '''
  2. read ZVI (Zeiss) image file
  3. - incomplete support
  4. - open uncompressed image from multi item image (Count>0)
  5. - require OleFileIO_PL - a Python module to read MS OLE2 files
  6. http://www.decalage.info/en/python/olefileio#attachments
  7. .. code-block:: python
  8. from zvi import zviread, getcount
  9. fname = '../../test/data/test1.zvi'
  10. n = getcount(fname)
  11. for s in getdir(fname):
  12. print s
  13. print 'Count = ',n
  14. for p in range(n):
  15. zvi = zviread(fname,p)
  16. x = zvi.Image.Array
  17. print x
  18. '''
  19. __author__ = 'olivier'
  20. import struct
  21. import numpy as np
  22. from collections import namedtuple
  23. import sys
  24. #sys.path.append('./dependencies')
  25. import OleFileIO_PL
  26. def i32(data):
  27. '''return int32 from len4 string'''
  28. low,high = struct.unpack('<hh',data[:4])
  29. return (high << 16) + low
  30. def print_hex(data,n = 16):
  31. print '|'.join(['%02x'%(ord(data[i])) for i in range(n)])
  32. def read_struct(data,t):
  33. '''read a t type from data(str)'''
  34. # vartype = (ord(data[0]),ord(data[1]))
  35. # print t,vartype
  36. next = data[2:] #skip vartype I16
  37. if t is '?':
  38. return [None,next]
  39. if t is 'EMPTY':
  40. return [None,next]
  41. if t is 'NULL':
  42. return [None,next]
  43. if t is 'I2':
  44. low = struct.unpack('<h',next[:2])
  45. return [low[0],next[2:]]
  46. if t is 'I4':
  47. r = i32(next[:4])
  48. return [r,next[4:]]
  49. if t is 'BLOB':
  50. size = i32(next[:4])
  51. r = next[4:4+size]
  52. return [r,next[4+size:]]
  53. if t is 'BSTR':
  54. #! 4 extra bytes escaped
  55. low,high = struct.unpack('<hh',next[:4])
  56. size = (high << 16) + low
  57. if size>0:
  58. s = struct.unpack('s',next[4:4+size])
  59. next = next[4+4+size:]
  60. else:
  61. s=''
  62. next = next[4+4:]
  63. return [s,next]
  64. raise ValueError('unknown type:%s'%type)
  65. ZviImageTuple = namedtuple('ZviImageTuple',
  66. 'Version FileName Width Height Depth PixelFormat Count \
  67. ValidBitsPerPixel m_PluginCLSID Others Layers Scaling')
  68. def read_image_container_content(stream):
  69. '''returns a ZviImageTuple from a stream'''
  70. data = stream.read()
  71. content = {}
  72. next = data
  73. [version,next] = read_struct(next,'I4')
  74. # [Type,next] = read_struct(next,'I4')
  75. # [TypeDescription,next] = read_struct(next,'BSTR')
  76. [filename,next] = read_struct(next,'BSTR')
  77. [width,next] = read_struct(next,'I4')
  78. [height,next] = read_struct(next,'I4')
  79. [depth,next] = read_struct(next,'I4')
  80. [pixel_format,next] = read_struct(next,'I4')
  81. [count,next] = read_struct(next,'I4')
  82. [valid_bits_per_pixel,next] = read_struct(next,'I4')
  83. [m_PluginCLSID,next] = read_struct(next,'I4')
  84. [others,next] = read_struct(next,'I4')
  85. [layers,next] = read_struct(next,'I4')
  86. [scaling,next] = read_struct(next,'I2')
  87. zvi_image = ZviImageTuple(version, filename, width, height, depth, pixel_format,
  88. count, valid_bits_per_pixel,m_PluginCLSID, others, layers, scaling)
  89. return zvi_image
  90. ZviItemTuple = namedtuple('ZviItemTuple',
  91. 'Version FileName Width Height Depth PixelFormat Count \
  92. ValidBitsPerPixel Others Layers Scaling Image')
  93. PixelFormat = {1:(3,'ByteBGR'),
  94. 2:(4,'ByteBGRA'),
  95. 3:(1,'Byte'),
  96. 4:(2,'Word'),
  97. 5:(4,'Long'),
  98. 6:(4,'Float'),
  99. 7:(8,'Double'),
  100. 8:(6,'WordBGR'),
  101. 9:(4,'LongBGR')}
  102. def read_item_storage_content(stream):
  103. '''returns ZviItemTuple from the stream'''
  104. data = stream.read()
  105. next = data
  106. [version,next] = read_struct(next,'I4')
  107. # [Type,next] = read_struct(next,'I4')
  108. # [TypeDescription,next] = read_struct(next,'BSTR')
  109. [filename,next] = read_struct(next,'BSTR')
  110. [width,next] = read_struct(next,'I4')
  111. [height,next] = read_struct(next,'I4')
  112. [depth,next] = read_struct(next,'I4')
  113. [pixel_format,next] = read_struct(next,'I4')
  114. [count,next] = read_struct(next,'I4')
  115. [valid_bits_per_pixel,next] = read_struct(next,'I4')
  116. [others,next] = read_struct(next,'BLOB')
  117. [layers,next] = read_struct(next,'BLOB')
  118. [scaling,next] = read_struct(next,'BLOB')
  119. # offset is image size + header size(28)
  120. offset = width*height*PixelFormat[pixel_format][0] + 28
  121. #parse the actual image data
  122. image = parse_image(data[-offset:])
  123. #group results into one single structure (namedtuple)
  124. zvi_item = ZviItemTuple(version, filename, width, height, depth, pixel_format,
  125. count, valid_bits_per_pixel, others, layers, scaling, image)
  126. return zvi_item
  127. ImageTuple = namedtuple('ImageTuple',
  128. 'Version Width Height Depth PixelWidth PixelFormat \
  129. ValidBitsPerPixel Array')
  130. def parse_image(data):
  131. '''returns ImageTuple from raw image data(header+image)'''
  132. version = i32(data[:4])
  133. width = i32(data[4:8])
  134. height = i32(data[8:12])
  135. depth = i32(data[12:16])
  136. pixel_width = i32(data[16:20])
  137. pixel_format = i32(data[20:24])
  138. valid_bits_per_pixel = i32(data[24:28])
  139. raw = np.fromstring(data[28:],'uint16')
  140. array = np.reshape(raw,(height,width))
  141. image = ImageTuple(version, width, height, depth, pixel_width, pixel_format,
  142. valid_bits_per_pixel, array)
  143. return image
  144. def getcount(filename):
  145. '''returns the number of image planes'''
  146. ole = OleFileIO_PL.OleFileIO(filename)
  147. s = ['Image', 'Contents']
  148. stream = ole.openstream(s)
  149. zvi_image = read_image_container_content(stream)
  150. return zvi_image.Count
  151. def getdir(filename):
  152. '''returns the content structure(streams) of the zvi file + length of each streams '''
  153. dir = []
  154. ole = OleFileIO_PL.OleFileIO(filename)
  155. for s in ole.listdir():
  156. stream = ole.openstream(s)
  157. dir.append('%10d %s'%(len(stream.read()),s))
  158. return dir
  159. def zviread(fname,plane):
  160. '''returns ZviItemTuple of the plane from zvi file fname'''
  161. ole = OleFileIO_PL.OleFileIO(fname)
  162. s = ['Image', 'Item(%d)'%plane, 'Contents']
  163. stream = ole.openstream(s)
  164. return read_item_storage_content(stream)
  165. def test():
  166. '''simple zvi 4 plane image reading'''
  167. fname = '../../test/data/test1.zvi'
  168. n = getcount(fname)
  169. for s in getdir(fname):
  170. print s
  171. print 'Count = ',n
  172. for p in range(n):
  173. zvi = zviread(fname,p)
  174. x = zvi.Image.Array
  175. print x
  176. if __name__ == "__main__":
  177. test()