【问题标题】:How do you go from a sip.voidptr (QImage.constBits()) to a ctypes void or char pointer?你如何从 sip.voidptr (QImage.constBits()) 到 ctypes void 或 char 指针?
【发布时间】:2017-12-07 07:28:52
【问题描述】:

我使用的是 python,当然你不能快速遍历大图像的每个像素,所以我使用 C DLL。

我想做这样的事情:

img = QImage("myimage.png").constBits()
imgPtr = c_void_p(img)
found = ctypesDLL.myImageSearchMethod(imgPtr, width, height)

但是这一行 imgPtr = c_void_p(img) 产量

builtins.TypeError: 无法转换为指针

我不需要修改这些位。请教我你在这方面的绝地之道。

【问题讨论】:

    标签: python python-3.x qt pyqt ctypes


    【解决方案1】:

    heresip.voidptr.__int__() 方法所述

    以整数形式返回地址

    c_void_p documentation

    构造函数接受一个可选的整数初始化器。

    所以你应该能够构建一个c_void_p,将sip.voidptr.__int__() 方法的返回值传递给它的构造函数:

    imgPtr = c_void_p(img.__int__())
    

    我以这种方式测试了这个解决方案:

    from PyQt5 import QtGui
    from ctypes import *
    
    lib = CDLL("/usr/lib/libtestlib.so")
    
    image = QtGui.QImage("so.png")
    bits = image.constBits()
    bytes = image.bytesPerLine()
    lib.f(c_void_p(bits.__int__()), c_int(image.width()), c_int(image.height()), c_int(bytes))
    

    这适用于以下功能:

    #include <cstdio>
    #include <QImage>
    extern "C" {
    
        void f(unsigned char * c, int width, int height, int bpl)
        {
            printf("W:%d H:%d BPL:%d\n", width, height, bpl);
            QImage image(c, width, height, bpl, QImage::Format_RGB32);
            image.save("test.bmp");
        }
    }
    

    【讨论】:

    • 谢谢,你是救世主!
    猜你喜欢
    • 1970-01-01
    • 2012-11-30
    • 2016-02-15
    • 1970-01-01
    • 2016-01-20
    • 2013-04-03
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    相关资源
    最近更新 更多