Opencv zbar python扫描程序错误

时间:2017-12-25 18:16:04

标签: python opencv zbar

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函数意味着什么。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

所有信用标记杰伊的评论。您的代码将如下所示

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

希望这有帮助!