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 )