2017-12-07 3 views
1

저는 파이썬을 사용하고 있습니다. 물론 큰 이미지의 모든 픽셀을 아주 빠르게 반복 할 수는 없으므로 C DLL을 사용합니다.어떻게 sip.voidptr (QImage.constBits())에서 ctypes void 또는 char 포인터로 이동합니까?

나는 이런 식으로 뭔가를하고 싶지 : 나는 비트를 수정할 필요가 없습니다

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

그러나이 줄 imgPtr = c_void_p (IMG) yelds

builtins.TypeError: cannot be converted to pointer

. 이 분야에서 제다이 방식을 가르쳐주세요. here 언급 한 바와 같이

답변

2

, c_void_pdocumentation

The constructor accepts an optional integer initializer.

그래서 당신은 생성자에 sip.voidptr.__int__() 메서드의 반환 값을 통과하는 c_void_p을 구축 할 수 있어야합니다 말한다

returns the address as an integer

동안 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"); 
    } 
} 
+0

덕분에, 당신은 구세주입니다! –

관련 문제