C enums revisited

Just found an easy way to make enums.

(LADYBUG_DISABLE,
   LADYBUG_EDGE_SENSING,
   LADYBUG_NEAREST_NEIGHBOR,
   LADYBUG_NEAREST_NEIGHBOR_FAST,
   LADYBUG_RIGOROUS,
   LADYBUG_DOWNSAMPLE4,
   LADYBUG_MONO,
   LADYBUG_HQLINEAR) = xrange(8)

This is a simple way to make enums. Overriden values need to be added separately.
Unless a method to retrieve the enum name from the integer is needed, in that case you make another list (or dict for unordered values) to retrieve the enum word. The namedtuple is the best way if you don’t want to put quotes around each word in a long list.
For a ctypes version, you can use map()

(...) = map(c_int, xrange(8))

I guess there really isn’t a need to go the reverse direction unless you want to read the value for debugging. In my novice understanding when first learning ctypes I thought it would be necessary to have this option. Here how to make a list and then enumerate from that list:

LadybugColorProcessingMethod = '''
   LADYBUG_DISABLE
   LADYBUG_EDGE_SENSING
   LADYBUG_NEAREST_NEIGHBOR
   LADYBUG_NEAREST_NEIGHBOR_FAST
   LADYBUG_RIGOROUS
   LADYBUG_DOWNSAMPLE4
   LADYBUG_MONO
   LADYBUG_HQLINEAR
   '''.split()
for n, v in zip( LadybugColorProcessingMethod, range(8) ):
    exec( n + ' = ' + str(v) )

A namedtuple implementation might be preferred if the same name might be used for different values in different enums.

PIL to OpenCV image

An example suggested this code:

        cv_img1 = cv.CreateImageHeader(img1.size, cv.IPL_DEPTH_8U, 1)
        cv.SetData(cv_img1, img1.tostring(), img1.width*3)

But the image kept getting stretch along the width. Playing with the numbers didn’t help. The above code is suppose to convert an RGB image to single band, but I finally decided to convert the PIL image to a single band first and then it worked. Remove the width multiplier.

        img1 = img1.convert('L')
        cv_img1 = cv.CreateImageHeader(img1.size, cv.IPL_DEPTH_8U, 1)
        cv.SetData(cv_img1, img1.tostring(), img1.width  )