使用pickle将数组[Byte]从ironpython传递到python
我最近开始为蔡司的ZEN Blue控制的显微镜编写宏程序。蔡司宏环境使用IronPython 2.7.2.1。我需要编写一个hdf5文件,但不幸的是,对IronPython的hdf5支持非常差 我要传输的数据类型为Array[Byte],它似乎以某种方式连接到.NET的CLR:使用pickle将数组[Byte]从ironpython传递到python,python,clr,ironpython,pickle,Python,Clr,Ironpython,Pickle,我最近开始为蔡司的ZEN Blue控制的显微镜编写宏程序。蔡司宏环境使用IronPython 2.7.2.1。我需要编写一个hdf5文件,但不幸的是,对IronPython的hdf5支持非常差 我要传输的数据类型为Array[Byte],它似乎以某种方式连接到.NET的CLR: print "length: %i type: %s" %(len(data), type(data)) 给出: length: 4915200 type: <type 'Array[Byte]'> 问题
print "length: %i type: %s" %(len(data), type(data))
给出:
length: 4915200 type: <type 'Array[Byte]'>
问题:将.NET数组[Byte]转换为未链接到.NET clr的基本python类型/对象的最佳(最快)方法是什么
非常感谢,,
Dominic经过一些测试,我发现最有效的解决方案如下: 首先,将数组[Byte]转换为python字符串:
data_str = str(buffer(data))
我不完全确定缓冲区的作用,但它似乎是高效计算所必需的,一些解释说
然后发送到cpython(在我的例子中,通过套接字,cpython在linux机器上运行)并转换为元组:
#data_str is an RGB image with dims sx and sy
format_str = str(sx*sy*3).strip() + 'B'
im_data = struct.unpack(format_str, data_str) #conversion to tuple
最后,使用numpy
和h5py
编写.h5
文件:
#put into np.array
im = np.asarray(im_data, dtype=np.uint8) #this is the most Time consuming part
newshape = (3, sx, sy)
im = np.reshape(im, newshape, order='F')
#write out
f = h5py.File('zenimage_bgr24.h5', 'w')
f.create_dataset('/im', data = im, dtype='uint8')
f.close()
#put into np.array
im = np.asarray(im_data, dtype=np.uint8) #this is the most Time consuming part
newshape = (3, sx, sy)
im = np.reshape(im, newshape, order='F')
#write out
f = h5py.File('zenimage_bgr24.h5', 'w')
f.create_dataset('/im', data = im, dtype='uint8')
f.close()