Fix Python – Convert image from PIL to openCV format


Asked By – md1hunox

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?

Now we will see solution for issue: Convert image from PIL to openCV format


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

This question is answered By – Abhishek Thakur

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0