convert openCV image into PIL Image in Python (for use with Zbar library)

后端 未结 4 596
长发绾君心
长发绾君心 2021-02-05 01:18

I\'m trying to use the Zbar library\'s QR code detection methods on images I extract with OpenCV\'s camera methods. Normally the QR code detection methods work with images (jpg,

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-05 01:40

    Are you trying to obtain a RGB image? If that is the case, you need to change your parameters from this:

    cv_im = cv.CreateImage((320,200), cv.IPL_DEPTH_8U, 1)
    pi = Image.fromstring("L", cv.GetSize(cv_im), cv_im.tostring())
    

    to that:

    cv_im = cv.CreateImage((320,200), cv.IPL_DEPTH_8U, 3)
    pi = Image.fromstring("RGB", cv.GetSize(cv_im), cv_im.tostring())
    

    since it is documented almost nowhere, but the 'L' parameter of Image.fromstring is for 8-bit B&W images. Besides, you need to change the argument of your cv.CreateImage function from 1 (single channel image) to 3 (3 channels=RGB).

    Hope it works for you. Cheers

提交回复
热议问题