c++ 调用 python 备忘

PyBytesObject 值的获取:

    PyObject *pFuncSetCredentialResult = PyObject_CallObject(pFuncSetCredential, pFuncSetCredentialArgs);
        PyBytesObject* pBytes = (PyBytesObject*)pFuncSetCredentialResult;
        char* data = new char[pBytes->ob_base.ob_size + 1];
        memcpy(data, pBytes->ob_sval, pBytes->ob_base.ob_size);
        FILE * pFile = fopen("C:\\xxxx\\myfile.tif", "wb");
        fwrite(data, sizeof(char), pBytes->ob_base.ob_size, pFile);
        fclose(pFile);

通过值构造新的 PyBytesObject:

FILE* f = fopen("C:\\xxx\\myfile.tif", "rb");
    fseek(f, 0, SEEK_END);
    long size = ftell(f);
    rewind(f);
    unsigned char* data = new unsigned char[size];
    fread(data, sizeof(char), size, f);
    fclose(f);

    PyBytesObject* pBytes = (PyBytesObject*)PyBytes_FromStringAndSize((const char*)data, (Py_ssize_t)size);
    char* data2 = new char[pBytes->ob_base.ob_size + 1];
    memcpy(data2, pBytes->ob_sval, pBytes->ob_base.ob_size);
    FILE * pFile = fopen("C:\\xxx\\myfile2.tif", "wb");
    fwrite(data2, sizeof(char), pBytes->ob_base.ob_size, pFile);
    fclose(pFile);

 其它示例:

void runPythonScriptDemo()
{
    cout << "started" << endl;

    if (!Py_IsInitialized())
        Py_Initialize();

    if (!Py_IsInitialized())
    {
        cout << "Py_IsInitialized failed" << endl;
        return;
    }

    cout << "Py_Initialized !" << endl;

    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.insert(0, 'C:/xxx/build')");
    PyRun_SimpleString("import main");
    PyRun_SimpleString("main.msgbox('python from c++')");

    Py_Finalize();
}

void runPythonModuleMethodDemo()
{
    cout << "started" << endl;

    if (!Py_IsInitialized())
        Py_Initialize();

    if (!Py_IsInitialized())
    {
        cout << "Py_IsInitialized failed" << endl;
        return;
    }

    cout << "Py_Initialized !" << endl;

    //设置路径起作用,说明脚本执行与动态调用在一个执行上下文
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.insert(0, 'C:/xxxx/build')");

    //从当前目录下获取模块
    PyObject *pModule = PyImport_ImportModule("main");

    //获取模块的字典,所有方法存在于字典中
    PyObject *pDict = PyModule_GetDict(pModule);

    //通知系统尽快回收pModule
    Py_DECREF(pModule);

    //从方法字典中查找方法
    PyObject *pFunc = PyDict_GetItemString(pDict, "msgbox");
    if (!pFunc || !PyCallable_Check(pFunc))
    {
        cout << "load function 'msgbox' failed" << endl;
        Py_Finalize();
        return;
    }

    //构建字符串输入参数
    PyObject *str = Py_BuildValue("s", "hello");

    PyObject *pArgs = PyTuple_New(1);
    PyTuple_SetItem(pArgs, 0, str);

    try
    {
        PyObject *pResult = PyObject_CallObject(pFunc, pArgs);
    }
    catch (const std::exception ex)
    {
        printf("call function error: %s", ex.what());
        Py_Finalize();
        return;
    }

    Py_Finalize();
}

void runCythonCppDemo()
{
    cout << "started" << endl;

    if (!Py_IsInitialized())
        Py_Initialize();

    if (!Py_IsInitialized())
    {
        cout << "Py_IsInitialized failed" << endl;
        return;
    }

    cout << "Py_Initialized !" << endl;

    PyRun_SimpleString("import os");
    PyRun_SimpleString("os.chdir('C:/xxx/build')");
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.insert(0, 'C:/xxx/build')");

    setEncodeKey(123);

    PyRun_SimpleString("import main");
    PyRun_SimpleString("main.showEncodeKey()");

    Py_Finalize();
}

void runRecognizeMainTestDemo()
{
    cout << "started" << endl;

    if (!Py_IsInitialized())
        Py_Initialize();

    if (!Py_IsInitialized())
    {
        cout << "Py_IsInitialized failed" << endl;
        return;
    }

    cout << "Py_Initialized !" << endl;

    //设置路径起作用,说明脚本执行与动态调用在一个执行上下文
    PyRun_SimpleString("import os");
    PyRun_SimpleString("os.chdir('C:/xxx/build')");
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.insert(0, 'C:/xxx/build')");

    //从当前目录下获取模块
    PyObject *pModule = PyImport_ImportModule("main");
    if (!pModule || pModule == NULL)
    {
        cout << "import module 'main' failed" << endl;
        Py_Finalize();
        return;
    }

    //获取模块的字典,所有方法存在于字典中
    PyObject *pDict = PyModule_GetDict(pModule);

    //通知系统尽快回收pModule
    Py_DECREF(pModule);

    //从方法字典中查找方法
    PyObject *pFunc = PyDict_GetItemString(pDict, "test");
    if (!pFunc || !PyCallable_Check(pFunc))
    {
        cout << "load function 'test' failed" << endl;
        Py_Finalize();
        return;
    }

    //无参构造
    PyObject *pArgs = PyTuple_New(0);

    try
    {
        PyObject *pResult = PyObject_CallObject(pFunc, pArgs);
    }
    catch (const std::exception ex)
    {
        printf("call function error: %s", ex.what());
        Py_Finalize();
        return;
    }

    Py_Finalize();
}

void runRecognizeReadFileTest()
{
    cout << "started" << endl;

    if (!Py_IsInitialized())
        Py_Initialize();

    if (!Py_IsInitialized())
    {
        cout << "Py_IsInitialized failed" << endl;
        return;
    }

    cout << "Py_Initialized !" << endl;

    //设置路径起作用,说明脚本执行与动态调用在一个执行上下文
    PyRun_SimpleString("import os");
    PyRun_SimpleString("os.chdir('C:/xxx/build')");
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.insert(0, 'C:/xxx/build')");

    //从当前目录下获取模块
    PyObject *pModule = PyImport_ImportModule("func_interface");
    if (!pModule || pModule == NULL)
    {
        cout << "import module 'func_interface' failed" << endl;
        Py_Finalize();
        return;
    }

    //获取模块的字典,所有方法存在于字典中
    PyObject *pDict = PyModule_GetDict(pModule);

    //通知系统尽快回收pModule
    Py_DECREF(pModule);

    //从方法字典中查找方法
    PyObject *pFunc = PyDict_GetItemString(pDict, "read_file");
    if (!pFunc || !PyCallable_Check(pFunc))
    {
        cout << "load function 'read_file' failed" << endl;
        Py_Finalize();
        return;
    }

    //入参构造
    PyObject *pArgs = PyTuple_New(1);
    PyObject *str = Py_BuildValue("s", "./images/00000009-cdbeefd1e5bc-4449-bc58-9d36975c288601.tif");
    PyTuple_SetItem(pArgs, 0, str);

    try
    {
        //接收python返回的byte数组
        PyObject *pResult = PyObject_CallObject(pFunc, pArgs);
        PyBytesObject* pBytes = (PyBytesObject*)pResult;
        char* data = new char[pBytes->ob_base.ob_size + 1];
        memcpy(data, pBytes->ob_sval, pBytes->ob_base.ob_size);
        FILE * pFile = fopen("C:\\Users\\Knife\\Desktop\\myfile.tif", "wb");
        fwrite(data, sizeof(char), pBytes->ob_base.ob_size, pFile);
        fclose(pFile);
        cout << "ok" << endl;
    }
    catch (const std::exception ex)
    {
        printf("call function 'read_file' error: %s", ex.what());
        Py_Finalize();
        return;
    }

    Py_Finalize();
}

 

posted on 2022-11-18 16:19  空明流光  阅读(121)  评论(0编辑  收藏  举报

导航