ITK/Python Wrapping: Difference between revisions

From KitwarePublic
< ITK
Jump to navigationJump to search
No edit summary
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
== Python Wrapping ==
{{Historical}}
 
= Python Wrapping =


This section describes how to wrap ITK classes for being used from Python.
This section describes how to wrap ITK classes for being used from Python.


Since slight differences exist depending on the platform where the wrapping is done, this section will describe those platforms independently.
== Step 1: Make sure the python headers are installed ==
 
=== Microsoft Windows ===
 
In order to wrap ITK for Python under Microsoft Windows you must start by gathering the following materials - at least this is the configuration with which I succeeded
 
# Python 2.4
# ITK CVS version posterior to July 10 2004 - I used 3.2.0
# CableSwig- I used 2.2


It is convenient to use CSwig from CVS and to check it out inside the source tree of ITK. However this is not necessary as the path to CableSwig can be set in the CMake configuration for ITK.
Python will need to be installed along with the Python development headers (Python.h).


CMake configuration:
If you need to specify a specific Python version or a custom location of the headers, the following CMake configuration variables can be modified:


# Compiler: I tried MS Visual Studio Express which failed. However, I think this because VS Express does not include the MS Windows SDK. I believe that installing that (free download from MS) and adding it to the "VC directories" paths correctly in VS Express will make it work. I have MS VC 2003 .NET as well, and both CableSwig and ITK compiled without errors.
* PYTHON_INCLUDE_DIR
* PYTHON_LIBRARY
* PYTHON_EXECUTABLE
* PY_SITE_PACKAGES_PATH


# Shared Libs ON
== Step 2: Turn on Python wrapping in the ITK configuration ==
# ITK_CSWIG_PYTHON ON
# Set the python paths (Advanced Values), don't worry about PYTHON_DEBUG_LIBRARY
# TK_INTERNAL_PATH is the path to tkWinInt.h - this file is found in the source distro of Tcl/Tk.
# ITK_EXPLICIT_INSTANTATION must be off.


Open ALL_BUILD in MSVC, select the configuration you want (Release, RelWithDebugInfo etc) and build for a long time.
Turn ''ON'' the CMake configuration ''ITK_WRAP_PYTHON''.


Compiling with NumArray:
== Step 3: Select the pixel types and dimensions to build ==


It seems the CMake configuration for this combination needs some manual fine-tuning: If you see an error that import_libnumarray() is needed in itkPyBuffer.txx and cannot be found it is because this macro is defined in libnumarray.h (part of numarray) which is nowhere included. I added #include <libnumarray.h> in itkPyBuffer.h and the path to it in the MSVC directories (Tools->Options) and it compiled fine.  
You can choose what pixel types and dimensions to build into the wrapped interface.  There is a trade-off between the number of pixel types and images dimensions supported and the amount of compilation time required and the size of the wrapping libraries.


Installation:
For the dimensions to support, set the CMake configuration variable ''ITK_WRAP_DIMS''.  This is a semi-colon delimited string containing the dimensions to support (Image dimensions, etc).  The default is ''2;3''.


Build the INSTALL target. This will install the binaries and the Python wrapper modules. If you receive a CMake error like "C:/packages/InsightToolkit-3.2.0/Wrapping/CSwig/cmake_install.cmake:31:
The pixel types to support are boolean variables in the pattern ''ITK_WRAP_<pixel type>''. For instance,
FILE INSTALL cannot find file "C:/packages/InsightToolkit-3.2.0/Wrapping/CSwig/Python/InsightToolkit.py" to install." that's because the compilation configuration subdir (e.g. Release) is not being included, so CMake can’t find the file. Edit the paths in the .cmake file, and (re-)installation moves along fine.


Once everything is installed correctly, go to the Lib\site-packages of your Python installation and create a text file named "InsightToolkit.pth". Add four lines like these -<br> <br>
* ''ITK_WRAP_float''
C:\\Program Files\\ITK\\bin<br>
* ''ITK_WRAP_rgb_unsigned_char''
C:\\Program Files\\ITK\\lib\\InsightToolkit<br>
* ''ITK_WRAP_vector_double''
C:\\Program Files\\ITK\\lib\\InsightToolkit\\python<br>
* ''ITK_WRAP_complex_double''
C:\\Program Files\\ITK\\lib\\InsightToolkit\\python\\Release<br><br>
(or replace C:\Program Files with the root of your ITK installation. Then add C:\Program Files\ITK\bin to the %PATH% global variable (and the path to python.exe)


To test, start a cmd shell, type "python" and at the >>> prompt, "from InsightToolkit import *". You should see no errors.


=== Linux ===


{{ITK/Template/Footer}}
{{ITK/Template/Footer}}

Latest revision as of 20:04, 8 August 2017

Python Wrapping

This section describes how to wrap ITK classes for being used from Python.

Step 1: Make sure the python headers are installed

Python will need to be installed along with the Python development headers (Python.h).

If you need to specify a specific Python version or a custom location of the headers, the following CMake configuration variables can be modified:

  • PYTHON_INCLUDE_DIR
  • PYTHON_LIBRARY
  • PYTHON_EXECUTABLE
  • PY_SITE_PACKAGES_PATH

Step 2: Turn on Python wrapping in the ITK configuration

Turn ON the CMake configuration ITK_WRAP_PYTHON.

Step 3: Select the pixel types and dimensions to build

You can choose what pixel types and dimensions to build into the wrapped interface. There is a trade-off between the number of pixel types and images dimensions supported and the amount of compilation time required and the size of the wrapping libraries.

For the dimensions to support, set the CMake configuration variable ITK_WRAP_DIMS. This is a semi-colon delimited string containing the dimensions to support (Image dimensions, etc). The default is 2;3.

The pixel types to support are boolean variables in the pattern ITK_WRAP_<pixel type>. For instance,

  • ITK_WRAP_float
  • ITK_WRAP_rgb_unsigned_char
  • ITK_WRAP_vector_double
  • ITK_WRAP_complex_double




ITK: [Welcome | Site Map]