title: How do I create an OpenCV image from a PIL image?
body: <p>I want to do some image processing with OpenCV (in Python), but I have to start with a PIL <code>Image</code> object, so I can't use the <code>cvLoadImage()</code> call, since that takes a filename.  </p>
  
  <p>This recipe (adapted from <a href="http://opencv.willowgarage.com/wiki/PythonInterface" rel="noreferrer">http://opencv.willowgarage.com/wiki/PythonInterface</a>) does not work because <code>cvSetData</code> complains <code>argument 2 of type 'void *'</code> . Any ideas?</p>
  
  <pre><code>from opencv.cv import *
  from PIL import Image
  
  pi = Image.open('foo.png')                       # PIL image
  ci = cvCreateImage(pi.size, IPL_DEPTH_8U, 1)     # OpenCV image
  data = pi.tostring()
  cvSetData(ci, data, len(data)) 
  </code></pre>
  
  <p>I think the last argument to the <code>cvSetData</code> is wrong too, but I am not sure what it should be.</p>