[SciPy-user] trying to use weave to access image data in C

Prabhu Ramachandran prabhu at aero.iitm.ernet.in
Wed Sep 29 13:16:45 CDT 2004


>>>>> "AD" == Al Danial writes:

    AD> I'm inspired by recent posts on using weave to include C code
    AD> within a Python program.  Does anyone have an example of how
    AD> one would access the bytes of an image (as loaded with imread)
    AD> within a weave C inline?

I've attached two files that illustrate two things:

 1. How do you what you wanted.  This is the first attachment.

 2. How to create an extension module using weave's ext_tools for
    this.  This is the second attachment and shows how you can easily
    build full-fledged extension modules with ext_tools.

    AD> 1. How should I declare the img variable in the C code so that
    AD>    it correctly binds to the Python variable of the same name?

Its magic!  You don't have to do anything.  weave figures out the
types by inspecting the locals() from where you called it.

    AD> 2. How would I step through the img variable in the C code?
    AD>    For example would the green value at pixel i,j be
    AD>    referenced as
    AD>       img[i][j][2]
    AD>    or
    AD>       img[i + j*rows][2]
    AD>    or
    AD>       img[(i + j*rows)*2]
    AD>    ?

Depends on how you weave the magic. If you use converters.blitz you
can simply use img(i,j,k).  If you don't use the blitz converter you
will only get a flattened array (i.e. a 1D array) from Numeric and
will need to do pointer arithmetic appropriately to access the
elements.

    AD> 3. This is the bonus round: if I modify the img variable in
    AD>    the C code how can I get the modified value back to Python?

If you set u(i, j, k) = 0 (or by using pointers), you are modifying
the contents of the array directly and nothing is necessary on your
side.  The modified img array will be what you see in Python.

cheers,
prabhu

-------------- next part --------------
#!/usr/bin/python
from scipy import imread
import weave
from weave import converters

img = imread('t.png')
cols, rows, colors = img.shape
print cols, rows

# Notice the following:
#  1. '\\n' to escape generating a newline in the C++ code.
#  2. Access array using img(i, j, k)    
#  3. The use of the type_converters=converters.blitz.  This is
#     what makes it possible to access img using img(i, j, k).    

code = """
   for (int j=0; j < cols; j++)
   {
       for (int i=0; i < rows; i++)
       {
           printf("img[%3d][%3d]=%3d %3d %3d\\n", 
                   i, j, img(i,j,0), img(i, j, 1), img(i, j, 2));
       }
   }
   """

weave.inline(code, ['img', 'rows', 'cols'],
             type_converters=converters.blitz)

-------------- next part --------------
#!/usr/bin/python
from scipy import imread
import weave
from weave import ext_tools, converters
import Numeric

def func_ext():

    # These effectively declare the types so the code generator works.
    rows, cols = 1, 1
    img = Numeric.ones((2,2,2), 'b')

    # create an extension module.
    mod = ext_tools.ext_module('test_img')

    # Notice the following:
    #  1. '\\n' to escape generating a newline in the C++ code.
    #  2. Access array using img(i, j, k)    
    #  3. The use of the type_converters=converters.blitz.  This is
    #     what makes it possible to access img using img(i, j, k).    
    
    code = """
       for (int j=0; j < cols; j++)
       {
           for (int i=0; i < rows; i++)
           {
               printf("img[%3d][%3d]=%3d %3d %3d\\n", 
                       i, j, img(i,j,0), img(i, j, 1), img(i, j, 2));
           }
       }
       """
    # build the function.
    func = ext_tools.ext_function('prn', code, ['img', 'rows', 'cols'],
                                  type_converters=converters.blitz)
    mod.add_function(func)

    # Compile it!
    mod.compile()

if __name__ == '__main__':
    try:
        import test_img
    except ImportError:
        func_ext()
        import test_img
    
    img = imread('t.png')
    cols, rows, colors = img.shape
    print cols, rows
    test_img.prn(img, rows, cols)


More information about the SciPy-user mailing list