2016-04-28 126 views
5

我正在创建一个使用Python OpenCV的项目。我的图像处理速度有点慢,所以我认为我可以通过创建一个.pyd文件(我在某处读取)来使代码更快。如何创建.pyd文件?

我能够使用Cython创建一个.c文件,但如何制作一个.pyd?虽然他们是一种.dll,我应该先制作一个.dll并将其转换吗?我认为它们不是平台无关的,Unix上的等价物是什么?

感谢您的帮助!

回答

3

您必须在终端中运行setup.py文件。这一个是使用numpy

try: 
    from setuptools import setup 
    from setuptools import Extension 
except ImportError: 
    from distutils.core import setup 
    from distutils.extension import Extension 

from Cython.Distutils import build_ext 
import numpy as np 

ext_modules = [Extension("my_code_cython",["my_code_cython.pyx"]), 
       Extension("another_code_cython",["another_code_cython.pyx"])] 

setup(
    name= 'Generic model class', 
    cmdclass = {'build_ext': build_ext}, 
    include_dirs = [np.get_include()], 
    ext_modules = ext_modules) 

在终端(在Windows CMD),你必须执行的命令的示例

python setup.py build_ext --inplace 

,我想你已经安装了编译器是很重要的(微软的Visual例如用于Python 2.7的C++编译器包)。你可以找到更多的信息在https://github.com/cython/cython/wiki/CythonExtensionsOnWindows

+0

谢谢你,我已经想通了:)。我可以在* nix上使用gcc创建'.so'文件吗? – linusg

+1

是的,这是我在Windows和* nix上使用的代码(在我的情况下是Ubuntu) – sebacastroh