Convert image from PIL to openCV format

I'm trying to convert image from PIL to OpenCV format. I'm using OpenCV 2.4.3. here is what I've attempted till now.

>>> from PIL import Image
>>> import cv2 as cv
>>> pimg ='D:\\traffic.jpg')                           #PIL Image
>>> cimg =,cv.IPL_DEPTH_8U,3)    #CV Image

But I think the image is not getting converted to CV format. The Window shows me a large brown image. Where am I going wrong in Converting image from PIL to CV format?

Also, why do I need to type to access functions?


  • Use this:

    pil_image ='Image.jpg').convert('RGB')
    open_cv_image = numpy.array(pil_image)
    # Convert RGB to BGR
    open_cv_image = open_cv_image[:, :, ::-1].copy()