使用pi将数组[Byte]从ironpython传递到python

2024-04-29 16:00:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近开始为一个显微镜编写宏程序,它由蔡司的ZEN Blue控制。蔡司宏环境使用IronPython 2.7.2.1。我需要编写一个hdf5文件,但不幸的是,hdf5对IronPython的支持非常糟糕。在

我要传输的数据类型是Array[Byte],它似乎以某种方式连接到.NET的CLR:

print "length: %i type: %s" %(len(data), type(data))

给出:

^{pr2}$

我可以使用pickle通过套接字连接成功地将包含int和字符串的dict传输到python服务器(建议使用here)。但是,当我试图取消Array[Byte]数据时,python希望导入CLR,这当然失败了:

ImportError: No module named clr

哪种方法最快链接到.NET对象?在

非常感谢, 多米尼克语


Tags: 文件datanet环境type方式bluebyte
1条回答
网友
1楼 · 发布于 2024-04-29 16:00:50

经过一些测试,我发现最有效的解决方案如下所示:

首先,将Array[Byte]转换为python字符串:

data_str = str(buffer(data))

我不完全确定buffer的作用,但它似乎是高效计算所必需的,一些解释here。在

然后发送到cpython(在我的例子中,cpython运行在linux机器上)并转换为tuple:

^{pr2}$

最后,使用numpyh5py来编写.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()

相关问题 更多 >