码迷,mamicode.com
首页 > 编程语言 > 详细

python 调用C的DLL案例

时间:2019-04-09 20:18:56      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:path   order   UNC   self   type   set   origin   str   style   

前言: python不能直接调用C++只能调用纯C的DLL

此处案例是python模仿opencv的cv2包,但是用c的DLL调用

 

import os
import csv
import time
import ctypes
from ctypes import *
opencv = CDLL("opencv_world310.dll")


class
IplTileInfo(Structure): _fields_ = [] class IplROI(Structure): _fields_ =[ (coi, c_int), (xOffset, c_int), (yOffset, c_int), (width, c_int), (height, c_int) ] class IplImage(Structure): def __repr__(self): res = [] for field in self._fields_: if field[0] in [imageData, imageDataOrigin]: continue res.append(%s=%s % (field[0], repr(getattr(self, field[0])))) return self.__class__.__name__ + ( + ,.join(res) + ) IplImage._fields_ = [ ("nSize",c_int), ("ID",c_int), ("nChannels",c_int), ("alphaChannel",c_int), ("depth",c_int), ("dataOrder",c_int), ("origin",c_int), ("align",c_int), ("width",c_int), ("height",c_int), ("imageSize",c_int), ("widthStep",c_int), ("BorderMode",c_int*4), ("BorderConst",c_int*4), ("colorModel",c_char*4), ("channelSeq",c_char*4), ("imageData",c_void_p), ("imageDataOrigin",c_char_p), ("imageId",c_void_p), ("roi", POINTER(IplROI)), ("maskROI", POINTER(IplImage)), ("tileInfo", POINTER(IplTileInfo))] loadimageFunc=opencv.cvLoadImage loadimageFunc.argtypes =[c_char_p,c_int] loadimageFunc.restype = POINTER(IplImage) cvimage = loadimageFunc(mpath,2).contents pt=POINTER(c_char) data=ctypes.cast(cvimage.imageData,pt) # 这里的转换非常重要

等同于

import os
import csv
import time
import cv2   #python opencv
import ctypes
from cv2 import *
from ctypes import *   

cvimage = cv2.imread(mpath, cv2.IMREAD_ANYDEPTH)
data=cvimage.ctypes.data_as(ctypes.POINTER(ctypes.c_char))

 

python 调用C的DLL案例

标签:path   order   UNC   self   type   set   origin   str   style   

原文地址:https://www.cnblogs.com/hahanonym/p/10679323.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!