How do I create an OpenCV image from a PIL image?

Posted by scrible on Stack Overflow See other posts from Stack Overflow or by scrible
Published on 2009-10-30T15:38:23Z Indexed on 2010/03/27 5:23 UTC
Read the original article Hit count: 323

Filed under:
|
|
|

I want to do some image processing with OpenCV (in Python), but I have to start with a PIL Image object, so I can't use the cvLoadImage() call, since that takes a filename.

This recipe (adapted from http://opencv.willowgarage.com/wiki/PythonInterface) does not work because cvSetData complains argument 2 of type 'void *' . Any ideas?

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))

I think the last argument to the cvSetData is wrong too, but I am not sure what it should be.

© Stack Overflow or respective owner

Related posts about python

Related posts about opencv