首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否可以将数据从void_pointer写入python中的文件?

是否可以将数据从void_pointer写入python中的文件?
EN

Stack Overflow用户
提问于 2013-07-01 23:58:59
回答 1查看 2.3K关注 0票数 1

我有一个C程序,它从我的相机中获取图像并将二进制数据写入文件。

到目前为止,我正在尝试编写一个python脚本来模仿C代码,但没有成功。

在我尝试将其写入文件之前,Python示例似乎没有错误地执行。换句话说,在Python中,fwrite无法在这个实例中写入要文件的数据。fwrite返回一个值0,指示已写入0文件。

所以,我要问的是:由于我有一个外部的c-函数,它读取二进制数据,大概是到内存中的某个缓冲区,所以我要问的是:的实例是否可能根本无法读取对这个内存位置的访问?如果是这样的话,python是否有可能获得访问内存中适当区域所需的权限?

作为参考,我包括了一个简化的C示例(按预期工作),后面跟着下面的Python示例,它打开并创建了适当的文件,但没有写入它。

代码语言:javascript
运行
复制
//Data Saving Sample
//The sample will open the first camera attached
//and acquire 3 frames.  The 3 frames will be saved
//in a raw format and can be opened with applications
//such as image-j

#define NUM_FRAMES  3
#define NO_TIMEOUT  -1

#include "stdio.h"
#include "picam.h"
#include <iostream>
using namespace std;

int main()
{

    // The following structure contains the image data
    AvailableData data;

    int readoutstride = 2097152;
    FILE *pFile;


    Acquire( NUM_FRAMES, NO_TIMEOUT, &data, &errors );

    pFile = fopen( "sampleX3.raw", "wb" );
    if( pFile )
    {
        fwrite( data.initial_readout, 1, (NUM_FRAMES*readoutstride), pFile );
        fclose( pFile );
    }

}

下面是我的Python版本:

代码语言:javascript
运行
复制
""" Load the picam.dll """
picamDll = 'DLLs/Picam.dll'
picam = ctypes.cdll.LoadLibrary(picamDll)

libc = ctypes.cdll.msvcrt
fopen = libc.fopen

data = AvailableData()
readoutstride = ctypes.c_int(2097152)

"""
This is the C-structure form from the provided header file, followed by the Python syntax for creating the same structure type
typedef struct AvailableData
{
    void* initial_readout;
    int64 readout_count;
} AvailableData;

class AvailableData(ctypes.Structure):
    _fields_ = [("initial_readout", ctypes.c_void_p), ("readout_count",ctypes.c_int64)]
"""

"""
Simplified C-Prototype for Acquire

Acquire(
int64                       readout_count,
int                       readout_time_out,
AvailableData*         available);
"""
picam.Acquire.argtypes = int64, int, ctypes.POINTER(AvailableData)

picam.Acquire.restype = int

print picam.Acquire( 3, -1, ctypes.byref(data), ctypes.byref(errors))

""" Write contents of Data to binary file """
fopen.argtypes = ctypes.c_char_p, ctypes.c_char_p
fopen.restype = ctypes.c_void_p

fwrite = libc.fwrite
fwrite.argtypes = ctypes.c_void_p, ctypes.c_size_t, ctypes.c_size_t, ctypes.c_void_p
fwrite.restype = ctypes.c_int

fclose = libc.fclose
fclose.argtypes = ctypes.c_void_p,
fclose.restype = ctypes.c_int

fp = fopen('PythonBinOutput.raw', 'wb')
print 'fwrite returns: ',fwrite(data.initial_readout, 3*readoutstride.value, 1, fp)
fclose(fp)
EN

回答 1

Stack Overflow用户

发布于 2013-07-02 02:22:30

创建一个用于常规Python文件的数组。例如:

获取一个空指针:

代码语言:javascript
运行
复制
class AvailableData(Structure):
    _fields_ = [
        ("initial_readout", c_void_p), 
    ]

cglobals = CDLL(None) # POSIX dlopen
cglobals.malloc.restype = c_void_p

size = 100  # 3 * readoutstride.value
data = AvailableData(cglobals.malloc(size))

现在将其用作数组:

代码语言:javascript
运行
复制
buf = (c_char * size).from_address(data.initial_readout)
with open('PythonBinOutput.raw', 'wb') as f: 
    f.write(buf)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17415460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档