【问题标题】:passing Array[Byte] from ironpython to python using pickle使用 pickle 将 Array[Byte] 从 ironpython 传递给 python
【发布时间】:2015-11-18 11:15:02
【问题描述】:

我最近开始为由蔡司 ZEN Blue 控制的显微镜编程宏。 Zeiss 宏环境使用 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]'>

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

ImportError: No module named clr

问题:将 .NET Array[Byte] 转换为未链接到 .NET clr 的基本 python 类型/对象的最佳(最快)方法是什么?

非常感谢, 多米尼克

【问题讨论】:

    标签: python clr ironpython pickle


    【解决方案1】:

    经过一些测试,我发现最有效的解决方案是这样的:

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

    data_str = str(buffer(data))
    

    我不完全确定 buffer 做了什么,但它似乎是高效计算所必需的,一些解释 here

    然后发送到 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
    

    最后,使用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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      相关资源
      最近更新 更多