调用C函数从蟒蛇
问题描述:
我在C有代码:调用C函数从蟒蛇
typedef struct {
int bottom;
int top;
int left;
int right;
} blur_rect;
int bitmapblur(
char* input,
char* output,
blur_rect* rects,
int count,
int blur_repetitions);
我需要使用bitmapblur功能从蟒蛇。 我该怎么做? 有关结构数组的问题。
THX
答
您首先需要使用ctypes。首先,构建一个结构:
import ctypes
class BlurRect(ctypes.Structure):
"""
rectangular area to blur
"""
_fields_ = [("bottom", ctypes.c_int),
("top", ctypes.c_int),
("left", ctypes.c_int),
("right", ctypes.c_int),
]
现在加载你的函数。您需要找出共享库的最佳名称 ,然后加载它。您应该已经将此代码实现为dll或.so,并且可以在ld路径中使用该代码 。
另一个棘手的问题是你的函数有一个“输出”参数,并且 函数有望在那里写出它的结果。你将需要为此创建一个缓冲区 。
的ctypes的代码会是这个样子:
blurlib = ctypes.cdll.LoadLibrary("libblur.so")
outbuf = ctypes.create_string_buffer(1024) # not sure how big you need this
inputStructs = [BlurRect(*x) for x in application_defined_data]
successFlag = blurlib.bitmapblur("input",
outbuf,
inputStructs,
count,
reps)
+0
[ctypes参考](http://docs.python.org/library/ctypes.html) – Cory
http://whathaveyoutried.com/ –
的问题是“如何创建结构数组中PYTON ???????” – Leonid
不,问题是“你有没有打算做任何研究,或者你期望被勺子?”。 –