Opencv zbar python 扫描仪错误

Opencv zbar python scanner error

import zbar
import Image
import cv2

# create a reader
scanner = zbar.ImageScanner()
# configure the reader
scanner.parse_config('enable')
#create video capture feed
cap = cv2.VideoCapture(0)

while(True):
    ret, cv = cap.read()
    cv = cv2.cvtColor(cv, cv2.COLOR_BGR2GRAY)
    pil = Image.fromarray(cv)
    width, height = pil.size
    raw = pil.tostring()
    # wrap image data
    image = zbar.Image(width, height, 'Y800', raw)

    # scan the image for barcodes
    scanner.scan(image)

    # extract results
    for symbol in image:
        # do something useful with results
        print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data

# clean up
print "/n ...Done"

我运行这个代码但是这显示这个错误

Traceback (most recent call last):
  File "/home/joeydash/Desktop/InterIIT-UAV-challenge/zbar/main.py", line 17, in <module>
    raw = pil.tostring()
  File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 695, in tostring
    "Please call tobytes() instead.")
Exception: tostring() has been removed. Please call tobytes() instead.

不知道image.tostring函数是什么意思。我该如何解决?

所有功劳都归功于 jay 的评论。您的代码将如下所示

import zbar
import Image
import cv2

# create a reader
scanner = zbar.ImageScanner()
# configure the reader
scanner.parse_config('enable')
#create video capture feed
cap = cv2.VideoCapture(0)

while(True):
    ret, cv = cap.read()
    cv = cv2.cvtColor(cv, cv2.COLOR_BGR2GRAY)
    pil = Image.fromarray(cv)
    width, height = pil.size
    raw = pil.tobytes()
    # wrap image data
    image = zbar.Image(width, height, 'Y800', raw)

    # scan the image for barcodes
    scanner.scan(image)

    # extract results
    for symbol in image:
        # do something useful with results
        print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data

# clean up
print "/n ...Done"

注意怎么行 raw = pil.tostring() 已更改为 raw = pil.tobytes()

希望对您有所帮助!