How do you go from a sip.voidptr (QImage.constBits()) to a ctypes void or char pointer?

2024/9/23 22:29:30

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 lineimgPtr = 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.

Answer

As stated here, sip.voidptr.__int__() method

returns the address as an integer

while c_void_p documentation says

The constructor accepts an optional integer initializer.

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");}
}
https://en.xdnf.cn/q/71774.html

Related Q&A

Just returning the text of elements in xpath (python / lxml)

I have an XML structure like this:mytree = """ <path><to><nodes><info>1</info><info>2</info><info>3</info></nodes></to> …

Python function parameter: tuple/list

My function expects a list or a tuple as a parameter. It doesnt really care which it is, all it does is pass it to another function that accepts either a list or tuple:def func(arg): # arg is tuple or …

Python binding for C++ operator overloading

I have a class similar to the following:class A {vector<double> v;double& x(int i) { return v[2*i]; }double& y(int i) { return v[2*i+1]; }double x(int i) const { return v[2*i]; }double y(…

python script to pickle entire environment

Im working inside the Python REPL, and I want to save my work periodically. Does anybody have a script to dump all the variables I have defined? Im looking for something like this:for o in dir():f=ope…

django-endless with class based views example

Im using Class Based Views for the first time. Im having trouble understating how using class based views I would implement django-endless-pagination twitter styling paging.Could I have an example of h…

Converting adjectives and adverbs to their noun forms

I am experimenting with word sense disambiguation using wordnet for my project. As a part of the project, I would like to convert a derived adjective or an adverb form to its root noun form.For exampl…

Sending a packet over physical loopback in scapy

Ive recently discovered Scapy & it looks wonderfulIm trying to look at simple traffic over a physical loopback module / stub on my NIC.But Scapy sniff doesnt give anythingWhat Im doing to send a pa…

Python Perfect Numbers

So I am supposed to write a Python program that will identify and print all the perfect numbers in some closed interval [ 2, n ], one per line. We only have to use nested while loops/ if-else statement…

Counting consecutive 1s in NumPy array

[1, 1, 1, 0, 0, 0, 1, 1, 0, 0]I have a NumPy array consisting of 0s and 1s like above. How can I add all consecutive 1s like below? Any time I encounter a 0, I reset.[1, 2, 3, 0, 0, 0, 1, 2, 0, 0]I ca…

python 3 replacement for dircache?

Before I go reinventing the wheel, can anyone tell me if theres a drop-in (or semi-drop-in) replacement for the single-line statement:allfiles = dircache.listdir(.)