Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using python and of course you can't loop through every pixel of a large image very quickly, so I defer to a C DLL.

I want to do something like this:

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

But this line imgPtr = c_void_p(img) yelds

builtins.TypeError: cannot be converted to pointer

I don't need to modify the bits. Please teach me your Jedi ways in this area.

So you should be able to build a c_void_p passing the return value of sip.voidptr.__int__() method to its constructor:

imgPtr = c_void_p(img.__int__())

I tested this solution this way:

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))

Which works fine with a function like:

#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");
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.