ITK/Python Wrapping: Difference between revisions

From KitwarePublic
< ITK
Jump to navigationJump to search
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Historical}}
= Python Wrapping =
= Python Wrapping =


Line 18: Line 20:
Turn ''ON'' the CMake configuration ''ITK_WRAP_PYTHON''.
Turn ''ON'' the CMake configuration ''ITK_WRAP_PYTHON''.


== CSWIG ==
== Step 3: Select the pixel types and dimensions to build ==
 
CSWIG is an older but still supported option.
 
=== Microsoft Windows CSWIG ===
 
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.
 
CMake configuration:
 
# 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.
 
# Shared Libs ON
# 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.
 
Compiling with NumArray:
 
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.
 
Installation:


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:
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.
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.


The next same issue is pythonfiles_install.cmake - the FILE(GLOB command on the first line also needs to read bin/Release/*.py ( or replace "Release" with your configuration).
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''.


This is still true as of CVS version 6/17/07.  
The pixel types to support are boolean variables in the pattern ''ITK_WRAP_<pixel type>''. For instance,


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]