2017-12-25 6 views
-1
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" 

私はこのコードを実行しましたが、これはこのエラーを示しOpenCVのzbar Pythonのスキャナエラー

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によって何を意味するのか分かりません。これをどうすれば解決できますか?

+5

あなたは例外が何をしているのか試しましたか? tostring() '' 'を' '' tobytes() '' 'に置き換えてください。 –

答えて

1

すべてのクレジットをjayのコメントにマークします。あなたのコードでは、行 raw = pil.tostring() は、このことができます raw = pil.tobytes()

希望に変更されているかに注意してください

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" 

次のようになります!